Home | History | Annotate | Line # | Download | only in i2c
ds1307.c revision 1.16
      1  1.16     matt /*	$NetBSD: ds1307.c,v 1.16 2012/07/25 03:07:37 matt Exp $	*/
      2   1.1  thorpej 
      3   1.1  thorpej /*
      4   1.1  thorpej  * Copyright (c) 2003 Wasabi Systems, Inc.
      5   1.1  thorpej  * All rights reserved.
      6   1.1  thorpej  *
      7   1.1  thorpej  * Written by Steve C. Woodford and Jason R. Thorpe for Wasabi Systems, Inc.
      8   1.1  thorpej  *
      9   1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     10   1.1  thorpej  * modification, are permitted provided that the following conditions
     11   1.1  thorpej  * are met:
     12   1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     13   1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     14   1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     16   1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     17   1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     18   1.1  thorpej  *    must display the following acknowledgement:
     19   1.1  thorpej  *      This product includes software developed for the NetBSD Project by
     20   1.1  thorpej  *      Wasabi Systems, Inc.
     21   1.1  thorpej  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22   1.1  thorpej  *    or promote products derived from this software without specific prior
     23   1.1  thorpej  *    written permission.
     24   1.1  thorpej  *
     25   1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26   1.1  thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27   1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28   1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29   1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30   1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31   1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32   1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33   1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34   1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35   1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     36   1.1  thorpej  */
     37   1.1  thorpej 
     38   1.9    lukem #include <sys/cdefs.h>
     39  1.16     matt __KERNEL_RCSID(0, "$NetBSD: ds1307.c,v 1.16 2012/07/25 03:07:37 matt Exp $");
     40   1.9    lukem 
     41   1.1  thorpej #include <sys/param.h>
     42   1.1  thorpej #include <sys/systm.h>
     43   1.1  thorpej #include <sys/device.h>
     44   1.1  thorpej #include <sys/kernel.h>
     45   1.1  thorpej #include <sys/fcntl.h>
     46   1.1  thorpej #include <sys/uio.h>
     47   1.1  thorpej #include <sys/conf.h>
     48   1.1  thorpej #include <sys/event.h>
     49   1.1  thorpej 
     50   1.1  thorpej #include <dev/clock_subr.h>
     51   1.1  thorpej 
     52   1.1  thorpej #include <dev/i2c/i2cvar.h>
     53   1.1  thorpej #include <dev/i2c/ds1307reg.h>
     54   1.1  thorpej 
     55  1.15     matt struct dsrtc_model {
     56  1.15     matt 	uint16_t dm_model;
     57  1.15     matt 	uint8_t dm_ch_reg;
     58  1.15     matt 	uint8_t dm_ch_value;
     59  1.15     matt 	uint8_t dm_rtc_start;
     60  1.15     matt 	uint8_t dm_rtc_size;
     61  1.15     matt 	uint8_t dm_nvram_start;
     62  1.15     matt 	uint8_t dm_nvram_size;
     63  1.15     matt 	uint8_t dm_flags;
     64  1.15     matt #define	DSRTC_FLAG_CLOCK_HOLD	1
     65  1.15     matt #define	DSRTC_FLAG_BCD		2
     66  1.15     matt };
     67  1.15     matt 
     68  1.15     matt static const struct dsrtc_model dsrtc_models[] = {
     69  1.15     matt 	{
     70  1.15     matt 		.dm_model = 1307,
     71  1.15     matt 		.dm_ch_reg = DSXXXX_SECONDS,
     72  1.15     matt 		.dm_ch_value = DS1307_SECONDS_CH,
     73  1.15     matt 		.dm_rtc_start = DS1307_RTC_START,
     74  1.15     matt 		.dm_rtc_size = DS1307_RTC_SIZE,
     75  1.15     matt 		.dm_nvram_start = DS1307_NVRAM_START,
     76  1.15     matt 		.dm_nvram_size = DS1307_NVRAM_SIZE,
     77  1.15     matt 		.dm_flags = DSRTC_FLAG_BCD | DSRTC_FLAG_CLOCK_HOLD,
     78  1.15     matt 	}, {
     79  1.15     matt 		.dm_model = 1339,
     80  1.15     matt 		.dm_rtc_start = DS1339_RTC_START,
     81  1.15     matt 		.dm_rtc_size = DS1339_RTC_SIZE,
     82  1.15     matt 		.dm_flags = DSRTC_FLAG_BCD,
     83  1.15     matt 	}, {
     84  1.15     matt 		.dm_model = 1672,
     85  1.15     matt 		.dm_rtc_start = DS1672_RTC_START,
     86  1.15     matt 		.dm_rtc_size = DS1672_RTC_SIZE,
     87  1.15     matt 		.dm_flags = 0,
     88  1.15     matt 	}, {
     89  1.15     matt 		.dm_model = 3232,
     90  1.15     matt 		.dm_rtc_start = DS3232_RTC_START,
     91  1.15     matt 		.dm_rtc_size = DS3232_RTC_SIZE,
     92  1.15     matt 		.dm_nvram_start = DS3232_NVRAM_START,
     93  1.15     matt 		.dm_nvram_size = DS3232_NVRAM_SIZE,
     94  1.15     matt 		.dm_flags = DSRTC_FLAG_BCD,
     95  1.15     matt 	},
     96  1.15     matt };
     97  1.15     matt 
     98   1.1  thorpej struct dsrtc_softc {
     99  1.11  xtraeme 	device_t sc_dev;
    100   1.1  thorpej 	i2c_tag_t sc_tag;
    101  1.15     matt 	uint8_t sc_address;
    102  1.15     matt 	bool sc_open;
    103  1.15     matt 	struct dsrtc_model sc_model;
    104   1.1  thorpej 	struct todr_chip_handle sc_todr;
    105   1.1  thorpej };
    106   1.1  thorpej 
    107  1.11  xtraeme static void	dsrtc_attach(device_t, device_t, void *);
    108  1.11  xtraeme static int	dsrtc_match(device_t, cfdata_t, void *);
    109   1.1  thorpej 
    110  1.11  xtraeme CFATTACH_DECL_NEW(dsrtc, sizeof(struct dsrtc_softc),
    111   1.1  thorpej     dsrtc_match, dsrtc_attach, NULL, NULL);
    112   1.1  thorpej extern struct cfdriver dsrtc_cd;
    113   1.1  thorpej 
    114   1.1  thorpej dev_type_open(dsrtc_open);
    115   1.1  thorpej dev_type_close(dsrtc_close);
    116   1.1  thorpej dev_type_read(dsrtc_read);
    117   1.1  thorpej dev_type_write(dsrtc_write);
    118   1.1  thorpej 
    119   1.1  thorpej const struct cdevsw dsrtc_cdevsw = {
    120   1.1  thorpej 	dsrtc_open, dsrtc_close, dsrtc_read, dsrtc_write, noioctl,
    121   1.8     cube 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER
    122   1.1  thorpej };
    123   1.1  thorpej 
    124  1.15     matt static int dsrtc_gettime_ymdhms(struct todr_chip_handle *, struct clock_ymdhms *);
    125  1.15     matt static int dsrtc_settime_ymdhms(struct todr_chip_handle *, struct clock_ymdhms *);
    126  1.15     matt static int dsrtc_clock_read_ymdhms(struct dsrtc_softc *, struct clock_ymdhms *);
    127  1.15     matt static int dsrtc_clock_write_ymdhms(struct dsrtc_softc *, struct clock_ymdhms *);
    128  1.15     matt 
    129  1.15     matt static int dsrtc_gettime_timeval(struct todr_chip_handle *, struct timeval *);
    130  1.15     matt static int dsrtc_settime_timeval(struct todr_chip_handle *, struct timeval *);
    131  1.15     matt static int dsrtc_clock_read_timeval(struct dsrtc_softc *, time_t *);
    132  1.15     matt static int dsrtc_clock_write_timeval(struct dsrtc_softc *, time_t);
    133  1.15     matt 
    134  1.15     matt static const struct dsrtc_model *
    135  1.15     matt dsrtc_model(u_int model)
    136  1.15     matt {
    137  1.15     matt 	/* no model given, assume it's a DS1307 (the first one) */
    138  1.15     matt 	if (model == 0)
    139  1.15     matt 		return &dsrtc_models[0];
    140  1.15     matt 
    141  1.15     matt 	for (const struct dsrtc_model *dm = dsrtc_models;
    142  1.15     matt 	     dm < dsrtc_models + __arraycount(dsrtc_models); dm++) {
    143  1.15     matt 		if (dm->dm_model == model)
    144  1.15     matt 			return dm;
    145  1.15     matt 	}
    146  1.15     matt 	return NULL;
    147  1.15     matt }
    148   1.1  thorpej 
    149   1.1  thorpej static int
    150  1.11  xtraeme dsrtc_match(device_t parent, cfdata_t cf, void *arg)
    151   1.1  thorpej {
    152   1.1  thorpej 	struct i2c_attach_args *ia = arg;
    153   1.1  thorpej 
    154  1.13      phx 	if (ia->ia_name) {
    155  1.13      phx 		/* direct config - check name */
    156  1.13      phx 		if (strcmp(ia->ia_name, "dsrtc") == 0)
    157  1.13      phx 			return 1;
    158  1.13      phx 	} else {
    159  1.13      phx 		/* indirect config - check typical address */
    160  1.13      phx 		if (ia->ia_addr == DS1307_ADDR)
    161  1.15     matt 			return dsrtc_model(cf->cf_flags & 0xffff) != NULL;
    162  1.13      phx 	}
    163  1.13      phx 	return 0;
    164   1.1  thorpej }
    165   1.1  thorpej 
    166   1.1  thorpej static void
    167  1.11  xtraeme dsrtc_attach(device_t parent, device_t self, void *arg)
    168   1.1  thorpej {
    169   1.5  thorpej 	struct dsrtc_softc *sc = device_private(self);
    170   1.1  thorpej 	struct i2c_attach_args *ia = arg;
    171  1.15     matt 	const struct dsrtc_model * const dm =
    172  1.15     matt 	    dsrtc_model(device_cfdata(self)->cf_flags);
    173   1.1  thorpej 
    174  1.15     matt 	aprint_naive(": Real-time Clock%s\n",
    175  1.15     matt 	    dm->dm_nvram_size > 0 ? "/NVRAM" : "");
    176  1.15     matt 	aprint_normal(": DS%u Real-time Clock%s\n", dm->dm_model,
    177  1.15     matt 	    dm->dm_nvram_size > 0 ? "/NVRAM" : "");
    178   1.1  thorpej 
    179   1.1  thorpej 	sc->sc_tag = ia->ia_tag;
    180   1.1  thorpej 	sc->sc_address = ia->ia_addr;
    181  1.15     matt 	sc->sc_model = *dm;
    182  1.11  xtraeme 	sc->sc_dev = self;
    183   1.1  thorpej 	sc->sc_open = 0;
    184   1.1  thorpej 	sc->sc_todr.cookie = sc;
    185  1.15     matt 	if (dm->dm_flags & DSRTC_FLAG_BCD) {
    186  1.15     matt 		sc->sc_todr.todr_gettime_ymdhms = dsrtc_gettime_ymdhms;
    187  1.15     matt 		sc->sc_todr.todr_settime_ymdhms = dsrtc_settime_ymdhms;
    188  1.15     matt 	} else {
    189  1.15     matt 		sc->sc_todr.todr_gettime = dsrtc_gettime_timeval;
    190  1.15     matt 		sc->sc_todr.todr_settime = dsrtc_settime_timeval;
    191  1.15     matt 	}
    192   1.1  thorpej 	sc->sc_todr.todr_setwen = NULL;
    193   1.1  thorpej 
    194   1.1  thorpej 	todr_attach(&sc->sc_todr);
    195   1.1  thorpej }
    196   1.1  thorpej 
    197   1.1  thorpej /*ARGSUSED*/
    198   1.1  thorpej int
    199   1.4      abs dsrtc_open(dev_t dev, int flag, int fmt, struct lwp *l)
    200   1.1  thorpej {
    201   1.1  thorpej 	struct dsrtc_softc *sc;
    202   1.1  thorpej 
    203  1.12  tsutsui 	if ((sc = device_lookup_private(&dsrtc_cd, minor(dev))) == NULL)
    204  1.14      phx 		return ENXIO;
    205   1.1  thorpej 
    206   1.1  thorpej 	/* XXX: Locking */
    207   1.1  thorpej 	if (sc->sc_open)
    208  1.14      phx 		return EBUSY;
    209   1.1  thorpej 
    210  1.15     matt 	sc->sc_open = true;
    211  1.14      phx 	return 0;
    212   1.1  thorpej }
    213   1.1  thorpej 
    214   1.1  thorpej /*ARGSUSED*/
    215   1.1  thorpej int
    216   1.4      abs dsrtc_close(dev_t dev, int flag, int fmt, struct lwp *l)
    217   1.1  thorpej {
    218   1.1  thorpej 	struct dsrtc_softc *sc;
    219   1.1  thorpej 
    220  1.12  tsutsui 	if ((sc = device_lookup_private(&dsrtc_cd, minor(dev))) == NULL)
    221  1.14      phx 		return ENXIO;
    222   1.1  thorpej 
    223  1.15     matt 	sc->sc_open = false;
    224  1.14      phx 	return 0;
    225   1.1  thorpej }
    226   1.1  thorpej 
    227   1.1  thorpej /*ARGSUSED*/
    228   1.1  thorpej int
    229   1.1  thorpej dsrtc_read(dev_t dev, struct uio *uio, int flags)
    230   1.1  thorpej {
    231   1.1  thorpej 	struct dsrtc_softc *sc;
    232  1.15     matt 	int error;
    233   1.1  thorpej 
    234  1.12  tsutsui 	if ((sc = device_lookup_private(&dsrtc_cd, minor(dev))) == NULL)
    235  1.14      phx 		return ENXIO;
    236   1.1  thorpej 
    237  1.15     matt 	const struct dsrtc_model * const dm = &sc->sc_model;
    238  1.15     matt 	if (uio->uio_offset >= dm->dm_nvram_size)
    239  1.14      phx 		return EINVAL;
    240   1.1  thorpej 
    241   1.1  thorpej 	if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0)
    242  1.14      phx 		return error;
    243   1.1  thorpej 
    244  1.15     matt 	KASSERT(uio->uio_offset >= 0);
    245  1.15     matt 	while (uio->uio_resid && uio->uio_offset < dm->dm_nvram_size) {
    246  1.15     matt 		uint8_t ch, cmd;
    247  1.15     matt 		const u_int a = uio->uio_offset;
    248  1.15     matt 		cmd = a + dm->dm_nvram_start;
    249  1.15     matt 		if ((error = iic_exec(sc->sc_tag,
    250  1.15     matt 		    uio->uio_resid > 1 ? I2C_OP_READ : I2C_OP_READ_WITH_STOP,
    251  1.15     matt 		    sc->sc_address, &cmd, 1, &ch, 1, 0)) != 0) {
    252   1.1  thorpej 			iic_release_bus(sc->sc_tag, 0);
    253  1.11  xtraeme 			aprint_error_dev(sc->sc_dev,
    254  1.16     matt 			    "%s: read failed at 0x%x: %d\n",
    255  1.16     matt 			    __func__, a, error);
    256  1.14      phx 			return error;
    257   1.1  thorpej 		}
    258   1.1  thorpej 		if ((error = uiomove(&ch, 1, uio)) != 0) {
    259   1.1  thorpej 			iic_release_bus(sc->sc_tag, 0);
    260  1.14      phx 			return error;
    261   1.1  thorpej 		}
    262   1.1  thorpej 	}
    263   1.1  thorpej 
    264   1.1  thorpej 	iic_release_bus(sc->sc_tag, 0);
    265   1.1  thorpej 
    266  1.14      phx 	return 0;
    267   1.1  thorpej }
    268   1.1  thorpej 
    269   1.1  thorpej /*ARGSUSED*/
    270   1.1  thorpej int
    271   1.1  thorpej dsrtc_write(dev_t dev, struct uio *uio, int flags)
    272   1.1  thorpej {
    273   1.1  thorpej 	struct dsrtc_softc *sc;
    274  1.15     matt 	int error;
    275   1.1  thorpej 
    276  1.12  tsutsui 	if ((sc = device_lookup_private(&dsrtc_cd, minor(dev))) == NULL)
    277  1.14      phx 		return ENXIO;
    278   1.1  thorpej 
    279  1.15     matt 	const struct dsrtc_model * const dm = &sc->sc_model;
    280  1.15     matt 	if (uio->uio_offset >= dm->dm_nvram_size)
    281  1.14      phx 		return EINVAL;
    282   1.1  thorpej 
    283   1.1  thorpej 	if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0)
    284  1.14      phx 		return error;
    285   1.1  thorpej 
    286  1.15     matt 	while (uio->uio_resid && uio->uio_offset < dm->dm_nvram_size) {
    287  1.15     matt 		uint8_t cmdbuf[2];
    288  1.15     matt 		const u_int a = (int)uio->uio_offset;
    289  1.15     matt 		cmdbuf[0] = a + dm->dm_nvram_start;
    290   1.1  thorpej 		if ((error = uiomove(&cmdbuf[1], 1, uio)) != 0)
    291   1.1  thorpej 			break;
    292   1.1  thorpej 
    293   1.1  thorpej 		if ((error = iic_exec(sc->sc_tag,
    294   1.1  thorpej 		    uio->uio_resid ? I2C_OP_WRITE : I2C_OP_WRITE_WITH_STOP,
    295   1.1  thorpej 		    sc->sc_address, cmdbuf, 1, &cmdbuf[1], 1, 0)) != 0) {
    296  1.11  xtraeme 			aprint_error_dev(sc->sc_dev,
    297  1.16     matt 			    "%s: write failed at 0x%x: %d\n",
    298  1.16     matt 			    __func__, a, error);
    299   1.1  thorpej 			break;
    300   1.1  thorpej 		}
    301   1.1  thorpej 	}
    302   1.1  thorpej 
    303   1.1  thorpej 	iic_release_bus(sc->sc_tag, 0);
    304   1.1  thorpej 
    305  1.14      phx 	return error;
    306   1.1  thorpej }
    307   1.1  thorpej 
    308   1.1  thorpej static int
    309  1.15     matt dsrtc_gettime_ymdhms(struct todr_chip_handle *ch, struct clock_ymdhms *dt)
    310   1.1  thorpej {
    311   1.1  thorpej 	struct dsrtc_softc *sc = ch->cookie;
    312   1.7  gdamore 	struct clock_ymdhms check;
    313   1.1  thorpej 	int retries;
    314   1.1  thorpej 
    315   1.7  gdamore 	memset(dt, 0, sizeof(*dt));
    316   1.1  thorpej 	memset(&check, 0, sizeof(check));
    317   1.1  thorpej 
    318   1.1  thorpej 	/*
    319   1.1  thorpej 	 * Since we don't support Burst Read, we have to read the clock twice
    320   1.1  thorpej 	 * until we get two consecutive identical results.
    321   1.1  thorpej 	 */
    322   1.1  thorpej 	retries = 5;
    323   1.1  thorpej 	do {
    324  1.15     matt 		dsrtc_clock_read_ymdhms(sc, dt);
    325  1.15     matt 		dsrtc_clock_read_ymdhms(sc, &check);
    326   1.7  gdamore 	} while (memcmp(dt, &check, sizeof(check)) != 0 && --retries);
    327   1.1  thorpej 
    328  1.14      phx 	return 0;
    329   1.1  thorpej }
    330   1.1  thorpej 
    331   1.1  thorpej static int
    332  1.15     matt dsrtc_settime_ymdhms(struct todr_chip_handle *ch, struct clock_ymdhms *dt)
    333   1.1  thorpej {
    334   1.1  thorpej 	struct dsrtc_softc *sc = ch->cookie;
    335   1.1  thorpej 
    336  1.15     matt 	if (dsrtc_clock_write_ymdhms(sc, dt) == 0)
    337  1.14      phx 		return -1;
    338   1.1  thorpej 
    339  1.14      phx 	return 0;
    340   1.1  thorpej }
    341   1.1  thorpej 
    342   1.1  thorpej static int
    343  1.15     matt dsrtc_clock_read_ymdhms(struct dsrtc_softc *sc, struct clock_ymdhms *dt)
    344   1.1  thorpej {
    345  1.15     matt 	struct dsrtc_model * const dm = &sc->sc_model;
    346  1.15     matt 	uint8_t bcd[DSXXXX_RTC_SIZE], cmdbuf[1];
    347  1.16     matt 	int error;
    348  1.15     matt 
    349  1.15     matt 	KASSERT(DSXXXX_RTC_SIZE >= dm->dm_rtc_size);
    350   1.1  thorpej 
    351  1.16     matt 	if ((error = iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) != 0) {
    352  1.11  xtraeme 		aprint_error_dev(sc->sc_dev,
    353  1.16     matt 		    "%s: failed to acquire I2C bus: %d\n",
    354  1.16     matt 		    __func__, error);
    355  1.14      phx 		return 0;
    356   1.1  thorpej 	}
    357   1.1  thorpej 
    358   1.1  thorpej 	/* Read each RTC register in order. */
    359  1.16     matt 	for (u_int i = 0; !error && i < dm->dm_rtc_size; i++) {
    360  1.15     matt 		cmdbuf[0] = dm->dm_rtc_start + i;
    361   1.1  thorpej 
    362  1.16     matt 		error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
    363  1.16     matt 		    sc->sc_address, cmdbuf, 1, &bcd[i], 1, I2C_F_POLL);
    364   1.1  thorpej 	}
    365   1.1  thorpej 
    366   1.1  thorpej 	/* Done with I2C */
    367   1.1  thorpej 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    368   1.1  thorpej 
    369  1.16     matt 	if (error != 0) {
    370  1.16     matt 		aprint_error_dev(sc->sc_dev,
    371  1.16     matt 		    "%s: failed to read rtc at 0x%x: %d\n",
    372  1.16     matt 		    __func__, cmdbuf[0], error);
    373  1.16     matt 		return 0;
    374  1.16     matt 	}
    375  1.16     matt 
    376   1.1  thorpej 	/*
    377  1.15     matt 	 * Convert the RTC's register values into something useable
    378   1.1  thorpej 	 */
    379  1.15     matt 	dt->dt_sec = FROMBCD(bcd[DSXXXX_SECONDS] & DSXXXX_SECONDS_MASK);
    380  1.15     matt 	dt->dt_min = FROMBCD(bcd[DSXXXX_MINUTES] & DSXXXX_MINUTES_MASK);
    381   1.1  thorpej 
    382  1.15     matt 	if ((bcd[DSXXXX_HOURS] & DSXXXX_HOURS_12HRS_MODE) != 0) {
    383  1.15     matt 		dt->dt_hour = FROMBCD(bcd[DSXXXX_HOURS] &
    384  1.15     matt 		    DSXXXX_HOURS_12MASK) % 12; /* 12AM -> 0, 12PM -> 12 */
    385  1.15     matt 		if (bcd[DSXXXX_HOURS] & DSXXXX_HOURS_12HRS_PM)
    386   1.1  thorpej 			dt->dt_hour += 12;
    387  1.14      phx 	} else
    388  1.15     matt 		dt->dt_hour = FROMBCD(bcd[DSXXXX_HOURS] &
    389  1.15     matt 		    DSXXXX_HOURS_24MASK);
    390   1.1  thorpej 
    391  1.15     matt 	dt->dt_day = FROMBCD(bcd[DSXXXX_DATE] & DSXXXX_DATE_MASK);
    392  1.15     matt 	dt->dt_mon = FROMBCD(bcd[DSXXXX_MONTH] & DSXXXX_MONTH_MASK);
    393   1.1  thorpej 
    394   1.1  thorpej 	/* XXX: Should be an MD way to specify EPOCH used by BIOS/Firmware */
    395  1.15     matt 	dt->dt_year = FROMBCD(bcd[DSXXXX_YEAR]) + POSIX_BASE_YEAR;
    396  1.15     matt 	if (bcd[DSXXXX_MONTH] & DSXXXX_MONTH_CENTURY)
    397  1.15     matt 		dt->dt_year += 100;
    398   1.1  thorpej 
    399  1.14      phx 	return 1;
    400   1.1  thorpej }
    401   1.1  thorpej 
    402   1.1  thorpej static int
    403  1.15     matt dsrtc_clock_write_ymdhms(struct dsrtc_softc *sc, struct clock_ymdhms *dt)
    404   1.1  thorpej {
    405  1.15     matt 	struct dsrtc_model * const dm = &sc->sc_model;
    406  1.15     matt 	uint8_t bcd[DSXXXX_RTC_SIZE], cmdbuf[2];
    407  1.16     matt 	int error;
    408  1.15     matt 
    409  1.15     matt 	KASSERT(DSXXXX_RTC_SIZE >= dm->dm_rtc_size);
    410   1.1  thorpej 
    411   1.1  thorpej 	/*
    412  1.15     matt 	 * Convert our time representation into something the DSXXXX
    413   1.1  thorpej 	 * can understand.
    414   1.1  thorpej 	 */
    415  1.15     matt 	bcd[DSXXXX_SECONDS] = TOBCD(dt->dt_sec);
    416  1.15     matt 	bcd[DSXXXX_MINUTES] = TOBCD(dt->dt_min);
    417  1.15     matt 	bcd[DSXXXX_HOURS] = TOBCD(dt->dt_hour); /* DSXXXX_HOURS_12HRS_MODE=0 */
    418  1.15     matt 	bcd[DSXXXX_DATE] = TOBCD(dt->dt_day);
    419  1.15     matt 	bcd[DSXXXX_DAY] = TOBCD(dt->dt_wday);
    420  1.15     matt 	bcd[DSXXXX_MONTH] = TOBCD(dt->dt_mon);
    421  1.15     matt 	bcd[DSXXXX_YEAR] = TOBCD((dt->dt_year - POSIX_BASE_YEAR) % 100);
    422  1.15     matt 	if (dt->dt_year - POSIX_BASE_YEAR >= 100)
    423  1.15     matt 		bcd[DSXXXX_MONTH] |= DSXXXX_MONTH_CENTURY;
    424   1.1  thorpej 
    425  1.16     matt 	if ((error = iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) != 0) {
    426  1.11  xtraeme 		aprint_error_dev(sc->sc_dev,
    427  1.16     matt 		    "%s: failed to acquire I2C bus: %d\n",
    428  1.16     matt 		    __func__, error);
    429  1.14      phx 		return 0;
    430   1.1  thorpej 	}
    431   1.1  thorpej 
    432   1.1  thorpej 	/* Stop the clock */
    433  1.15     matt 	cmdbuf[0] = dm->dm_ch_reg;
    434  1.15     matt 
    435  1.16     matt 	if ((error = iic_exec(sc->sc_tag, I2C_OP_READ, sc->sc_address,
    436  1.16     matt 	    cmdbuf, 1, &cmdbuf[1], 1, I2C_F_POLL)) != 0) {
    437  1.15     matt 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
    438  1.15     matt 		aprint_error_dev(sc->sc_dev,
    439  1.16     matt 		    "%s: failed to read Hold Clock: %d\n",
    440  1.16     matt 		    __func__, error);
    441  1.15     matt 		return 0;
    442  1.15     matt 	}
    443  1.15     matt 
    444  1.15     matt 	cmdbuf[1] |= dm->dm_ch_value;
    445   1.1  thorpej 
    446  1.16     matt 	if ((error = iic_exec(sc->sc_tag, I2C_OP_WRITE, sc->sc_address,
    447  1.16     matt 	    cmdbuf, 1, &cmdbuf[1], 1, I2C_F_POLL)) != 0) {
    448   1.1  thorpej 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
    449  1.11  xtraeme 		aprint_error_dev(sc->sc_dev,
    450  1.16     matt 		    "%s: failed to write Hold Clock: %d\n",
    451  1.16     matt 		    __func__, error);
    452  1.14      phx 		return 0;
    453   1.1  thorpej 	}
    454   1.1  thorpej 
    455   1.1  thorpej 	/*
    456   1.1  thorpej 	 * Write registers in reverse order. The last write (to the Seconds
    457   1.1  thorpej 	 * register) will undo the Clock Hold, above.
    458   1.1  thorpej 	 */
    459  1.15     matt 	uint8_t op = I2C_OP_WRITE;
    460  1.15     matt 	for (signed int i = dm->dm_rtc_size - 1; i >= 0; i--) {
    461  1.15     matt 		cmdbuf[0] = dm->dm_rtc_start + i;
    462  1.15     matt 		if (dm->dm_rtc_start + i == dm->dm_ch_reg) {
    463  1.15     matt 			op = I2C_OP_WRITE_WITH_STOP;
    464  1.15     matt 		}
    465  1.16     matt 		if ((error = iic_exec(sc->sc_tag, op, sc->sc_address,
    466  1.16     matt 		    cmdbuf, 1, &bcd[i], 1, I2C_F_POLL)) != 0) {
    467   1.1  thorpej 			iic_release_bus(sc->sc_tag, I2C_F_POLL);
    468  1.11  xtraeme 			aprint_error_dev(sc->sc_dev,
    469  1.16     matt 			    "%s: failed to write rtc at 0x%x: %d\n",
    470  1.16     matt 			    __func__, i, error);
    471   1.1  thorpej 			/* XXX: Clock Hold is likely still asserted! */
    472  1.14      phx 			return 0;
    473   1.1  thorpej 		}
    474   1.1  thorpej 	}
    475  1.15     matt 	/*
    476  1.15     matt 	 * If the clock hold register isn't the same register as seconds,
    477  1.15     matt 	 * we need to reeanble the clock.
    478  1.15     matt 	 */
    479  1.15     matt 	if (op != I2C_OP_WRITE_WITH_STOP) {
    480  1.15     matt 		cmdbuf[0] = dm->dm_ch_reg;
    481  1.15     matt 		cmdbuf[1] &= ~dm->dm_ch_value;
    482  1.15     matt 
    483  1.16     matt 		if ((error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
    484  1.16     matt 		    sc->sc_address, cmdbuf, 1, &cmdbuf[1], 1,
    485  1.16     matt 		    I2C_F_POLL)) != 0) {
    486  1.15     matt 			iic_release_bus(sc->sc_tag, I2C_F_POLL);
    487  1.15     matt 			aprint_error_dev(sc->sc_dev,
    488  1.16     matt 			    "%s: failed to Hold Clock: %d\n",
    489  1.16     matt 			    __func__, error);
    490  1.15     matt 			return 0;
    491  1.15     matt 		}
    492  1.15     matt 	}
    493   1.1  thorpej 
    494   1.1  thorpej 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    495   1.1  thorpej 
    496  1.14      phx 	return 1;
    497   1.1  thorpej }
    498  1.15     matt 
    499  1.15     matt static int
    500  1.15     matt dsrtc_gettime_timeval(struct todr_chip_handle *ch, struct timeval *tv)
    501  1.15     matt {
    502  1.15     matt 	struct dsrtc_softc *sc = ch->cookie;
    503  1.15     matt 	struct timeval check;
    504  1.15     matt 	int retries;
    505  1.15     matt 
    506  1.15     matt 	memset(tv, 0, sizeof(*tv));
    507  1.15     matt 	memset(&check, 0, sizeof(check));
    508  1.15     matt 
    509  1.15     matt 	/*
    510  1.15     matt 	 * Since we don't support Burst Read, we have to read the clock twice
    511  1.15     matt 	 * until we get two consecutive identical results.
    512  1.15     matt 	 */
    513  1.15     matt 	retries = 5;
    514  1.15     matt 	do {
    515  1.15     matt 		dsrtc_clock_read_timeval(sc, &tv->tv_sec);
    516  1.15     matt 		dsrtc_clock_read_timeval(sc, &check.tv_sec);
    517  1.15     matt 	} while (memcmp(tv, &check, sizeof(check)) != 0 && --retries);
    518  1.15     matt 
    519  1.15     matt 	return 0;
    520  1.15     matt }
    521  1.15     matt 
    522  1.15     matt static int
    523  1.15     matt dsrtc_settime_timeval(struct todr_chip_handle *ch, struct timeval *tv)
    524  1.15     matt {
    525  1.15     matt 	struct dsrtc_softc *sc = ch->cookie;
    526  1.15     matt 
    527  1.15     matt 	if (dsrtc_clock_write_timeval(sc, tv->tv_sec) == 0)
    528  1.15     matt 		return -1;
    529  1.15     matt 
    530  1.15     matt 	return 0;
    531  1.15     matt }
    532  1.15     matt 
    533  1.15     matt /*
    534  1.15     matt  * The RTC probably has a nice Clock Burst Read/Write command, but we can't use
    535  1.15     matt  * it, since some I2C controllers don't support anything other than single-byte
    536  1.15     matt  * transfers.
    537  1.15     matt  */
    538  1.15     matt static int
    539  1.15     matt dsrtc_clock_read_timeval(struct dsrtc_softc *sc, time_t *tp)
    540  1.15     matt {
    541  1.15     matt 	const struct dsrtc_model * const dm = &sc->sc_model;
    542  1.15     matt 	uint8_t buf[4];
    543  1.16     matt 	int error;
    544  1.15     matt 
    545  1.16     matt 	if ((error = iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) != 0) {
    546  1.16     matt 		aprint_error_dev(sc->sc_dev,
    547  1.16     matt 		    "%s: failed to acquire I2C bus: %d\n",
    548  1.16     matt 		    __func__, error);
    549  1.16     matt 		return 0;
    550  1.15     matt 	}
    551  1.15     matt 
    552  1.15     matt 	/* read all registers: */
    553  1.15     matt 	uint8_t reg = dm->dm_rtc_start;
    554  1.16     matt 	error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_address,
    555  1.16     matt 	     &reg, 1, buf, 4, I2C_F_POLL);
    556  1.15     matt 
    557  1.15     matt 	/* Done with I2C */
    558  1.15     matt 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    559  1.15     matt 
    560  1.16     matt 	if (error != 0) {
    561  1.16     matt 		aprint_error_dev(sc->sc_dev,
    562  1.16     matt 		    "%s: failed to read rtc at 0x%x: %d\n",
    563  1.16     matt 		    __func__, reg, error);
    564  1.16     matt 		return 0;
    565  1.16     matt 	}
    566  1.16     matt 
    567  1.15     matt 	uint32_t v = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
    568  1.15     matt 	*tp = v;
    569  1.15     matt 
    570  1.15     matt 	aprint_debug_dev(sc->sc_dev, "%s: cntr=0x%08"PRIx32"\n",
    571  1.15     matt 	    __func__, v);
    572  1.15     matt 
    573  1.16     matt 	return 1;
    574  1.15     matt }
    575  1.15     matt 
    576  1.15     matt static int
    577  1.15     matt dsrtc_clock_write_timeval(struct dsrtc_softc *sc, time_t t)
    578  1.15     matt {
    579  1.15     matt 	const struct dsrtc_model * const dm = &sc->sc_model;
    580  1.15     matt 	size_t buflen = dm->dm_rtc_size + 2;
    581  1.15     matt 	uint8_t buf[buflen];
    582  1.16     matt 	int error;
    583  1.15     matt 
    584  1.15     matt 	KASSERT((dm->dm_flags & DSRTC_FLAG_CLOCK_HOLD) == 0);
    585  1.15     matt 	KASSERT(dm->dm_ch_reg == dm->dm_rtc_start + 4);
    586  1.15     matt 
    587  1.15     matt 	buf[0] = dm->dm_rtc_start;
    588  1.15     matt 	buf[1] = (t >> 0) & 0xff;
    589  1.15     matt 	buf[2] = (t >> 8) & 0xff;
    590  1.15     matt 	buf[3] = (t >> 16) & 0xff;
    591  1.15     matt 	buf[4] = (t >> 24) & 0xff;
    592  1.15     matt 	buf[5] = 0;
    593  1.15     matt 
    594  1.16     matt 	if ((error = iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) != 0) {
    595  1.16     matt 		aprint_error_dev(sc->sc_dev,
    596  1.16     matt 		    "%s: failed to acquire I2C bus: %d\n",
    597  1.16     matt 		    __func__, error);
    598  1.16     matt 		return 0;
    599  1.15     matt 	}
    600  1.15     matt 
    601  1.16     matt 	error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address,
    602  1.16     matt 	    &buf, buflen, NULL, 0, I2C_F_POLL);
    603  1.16     matt 
    604  1.16     matt 	/* Done with I2C */
    605  1.16     matt 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    606  1.16     matt 
    607  1.15     matt 	/* send data */
    608  1.16     matt 	if (error != 0) {
    609  1.16     matt 		aprint_error_dev(sc->sc_dev,
    610  1.16     matt 		    "%s: failed to set time: %d\n",
    611  1.16     matt 		    __func__, error);
    612  1.16     matt 		return 0;
    613  1.15     matt 	}
    614  1.15     matt 
    615  1.16     matt 	return 1;
    616  1.15     matt }
    617