Home | History | Annotate | Line # | Download | only in i2c
ds1307.c revision 1.22
      1  1.22    bouyer /*	$NetBSD: ds1307.c,v 1.22 2016/04/05 10:53:16 bouyer 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.22    bouyer __KERNEL_RCSID(0, "$NetBSD: ds1307.c,v 1.22 2016/04/05 10:53:16 bouyer 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.19  macallan #include <dev/sysmon/sysmonvar.h>
     55   1.1   thorpej 
     56  1.15      matt struct dsrtc_model {
     57  1.15      matt 	uint16_t dm_model;
     58  1.15      matt 	uint8_t dm_ch_reg;
     59  1.15      matt 	uint8_t dm_ch_value;
     60  1.15      matt 	uint8_t dm_rtc_start;
     61  1.15      matt 	uint8_t dm_rtc_size;
     62  1.15      matt 	uint8_t dm_nvram_start;
     63  1.15      matt 	uint8_t dm_nvram_size;
     64  1.15      matt 	uint8_t dm_flags;
     65  1.15      matt #define	DSRTC_FLAG_CLOCK_HOLD	1
     66  1.15      matt #define	DSRTC_FLAG_BCD		2
     67  1.19  macallan #define	DSRTC_FLAG_TEMP		4
     68  1.15      matt };
     69  1.15      matt 
     70  1.15      matt static const struct dsrtc_model dsrtc_models[] = {
     71  1.15      matt 	{
     72  1.15      matt 		.dm_model = 1307,
     73  1.15      matt 		.dm_ch_reg = DSXXXX_SECONDS,
     74  1.15      matt 		.dm_ch_value = DS1307_SECONDS_CH,
     75  1.15      matt 		.dm_rtc_start = DS1307_RTC_START,
     76  1.15      matt 		.dm_rtc_size = DS1307_RTC_SIZE,
     77  1.15      matt 		.dm_nvram_start = DS1307_NVRAM_START,
     78  1.15      matt 		.dm_nvram_size = DS1307_NVRAM_SIZE,
     79  1.15      matt 		.dm_flags = DSRTC_FLAG_BCD | DSRTC_FLAG_CLOCK_HOLD,
     80  1.15      matt 	}, {
     81  1.15      matt 		.dm_model = 1339,
     82  1.15      matt 		.dm_rtc_start = DS1339_RTC_START,
     83  1.15      matt 		.dm_rtc_size = DS1339_RTC_SIZE,
     84  1.15      matt 		.dm_flags = DSRTC_FLAG_BCD,
     85  1.15      matt 	}, {
     86  1.15      matt 		.dm_model = 1672,
     87  1.15      matt 		.dm_rtc_start = DS1672_RTC_START,
     88  1.15      matt 		.dm_rtc_size = DS1672_RTC_SIZE,
     89  1.22    bouyer 		.dm_ch_reg = DS1672_CONTROL,
     90  1.22    bouyer 		.dm_ch_value = DS1672_CONTROL_CH,
     91  1.15      matt 		.dm_flags = 0,
     92  1.15      matt 	}, {
     93  1.19  macallan 		.dm_model = 3231,
     94  1.19  macallan 		.dm_rtc_start = DS3232_RTC_START,
     95  1.19  macallan 		.dm_rtc_size = DS3232_RTC_SIZE,
     96  1.19  macallan 		/*
     97  1.19  macallan 		 * XXX
     98  1.19  macallan 		 * the DS3232 likely has the temperature sensor too but I can't
     99  1.19  macallan 		 * easily verify or test that right now
    100  1.19  macallan 		 */
    101  1.19  macallan 		.dm_flags = DSRTC_FLAG_BCD | DSRTC_FLAG_TEMP,
    102  1.19  macallan 	}, {
    103  1.15      matt 		.dm_model = 3232,
    104  1.15      matt 		.dm_rtc_start = DS3232_RTC_START,
    105  1.15      matt 		.dm_rtc_size = DS3232_RTC_SIZE,
    106  1.15      matt 		.dm_nvram_start = DS3232_NVRAM_START,
    107  1.15      matt 		.dm_nvram_size = DS3232_NVRAM_SIZE,
    108  1.15      matt 		.dm_flags = DSRTC_FLAG_BCD,
    109  1.15      matt 	},
    110  1.15      matt };
    111  1.15      matt 
    112   1.1   thorpej struct dsrtc_softc {
    113  1.11   xtraeme 	device_t sc_dev;
    114   1.1   thorpej 	i2c_tag_t sc_tag;
    115  1.15      matt 	uint8_t sc_address;
    116  1.15      matt 	bool sc_open;
    117  1.15      matt 	struct dsrtc_model sc_model;
    118   1.1   thorpej 	struct todr_chip_handle sc_todr;
    119  1.19  macallan 	struct sysmon_envsys *sc_sme;
    120  1.19  macallan 	envsys_data_t sc_sensor;
    121   1.1   thorpej };
    122   1.1   thorpej 
    123  1.11   xtraeme static void	dsrtc_attach(device_t, device_t, void *);
    124  1.11   xtraeme static int	dsrtc_match(device_t, cfdata_t, void *);
    125   1.1   thorpej 
    126  1.11   xtraeme CFATTACH_DECL_NEW(dsrtc, sizeof(struct dsrtc_softc),
    127   1.1   thorpej     dsrtc_match, dsrtc_attach, NULL, NULL);
    128   1.1   thorpej extern struct cfdriver dsrtc_cd;
    129   1.1   thorpej 
    130   1.1   thorpej dev_type_open(dsrtc_open);
    131   1.1   thorpej dev_type_close(dsrtc_close);
    132   1.1   thorpej dev_type_read(dsrtc_read);
    133   1.1   thorpej dev_type_write(dsrtc_write);
    134   1.1   thorpej 
    135   1.1   thorpej const struct cdevsw dsrtc_cdevsw = {
    136  1.17  dholland 	.d_open = dsrtc_open,
    137  1.17  dholland 	.d_close = dsrtc_close,
    138  1.17  dholland 	.d_read = dsrtc_read,
    139  1.17  dholland 	.d_write = dsrtc_write,
    140  1.17  dholland 	.d_ioctl = noioctl,
    141  1.17  dholland 	.d_stop = nostop,
    142  1.17  dholland 	.d_tty = notty,
    143  1.17  dholland 	.d_poll = nopoll,
    144  1.17  dholland 	.d_mmap = nommap,
    145  1.17  dholland 	.d_kqfilter = nokqfilter,
    146  1.18  dholland 	.d_discard = nodiscard,
    147  1.17  dholland 	.d_flag = D_OTHER
    148   1.1   thorpej };
    149   1.1   thorpej 
    150  1.15      matt static int dsrtc_gettime_ymdhms(struct todr_chip_handle *, struct clock_ymdhms *);
    151  1.15      matt static int dsrtc_settime_ymdhms(struct todr_chip_handle *, struct clock_ymdhms *);
    152  1.15      matt static int dsrtc_clock_read_ymdhms(struct dsrtc_softc *, struct clock_ymdhms *);
    153  1.15      matt static int dsrtc_clock_write_ymdhms(struct dsrtc_softc *, struct clock_ymdhms *);
    154  1.15      matt 
    155  1.15      matt static int dsrtc_gettime_timeval(struct todr_chip_handle *, struct timeval *);
    156  1.15      matt static int dsrtc_settime_timeval(struct todr_chip_handle *, struct timeval *);
    157  1.15      matt static int dsrtc_clock_read_timeval(struct dsrtc_softc *, time_t *);
    158  1.15      matt static int dsrtc_clock_write_timeval(struct dsrtc_softc *, time_t);
    159  1.15      matt 
    160  1.19  macallan static int dsrtc_read_temp(struct dsrtc_softc *, uint32_t *);
    161  1.19  macallan static void dsrtc_refresh(struct sysmon_envsys *, envsys_data_t *);
    162  1.19  macallan 
    163  1.15      matt static const struct dsrtc_model *
    164  1.15      matt dsrtc_model(u_int model)
    165  1.15      matt {
    166  1.15      matt 	/* no model given, assume it's a DS1307 (the first one) */
    167  1.15      matt 	if (model == 0)
    168  1.15      matt 		return &dsrtc_models[0];
    169  1.15      matt 
    170  1.15      matt 	for (const struct dsrtc_model *dm = dsrtc_models;
    171  1.15      matt 	     dm < dsrtc_models + __arraycount(dsrtc_models); dm++) {
    172  1.15      matt 		if (dm->dm_model == model)
    173  1.15      matt 			return dm;
    174  1.15      matt 	}
    175  1.15      matt 	return NULL;
    176  1.15      matt }
    177   1.1   thorpej 
    178   1.1   thorpej static int
    179  1.11   xtraeme dsrtc_match(device_t parent, cfdata_t cf, void *arg)
    180   1.1   thorpej {
    181   1.1   thorpej 	struct i2c_attach_args *ia = arg;
    182   1.1   thorpej 
    183  1.13       phx 	if (ia->ia_name) {
    184  1.13       phx 		/* direct config - check name */
    185  1.13       phx 		if (strcmp(ia->ia_name, "dsrtc") == 0)
    186  1.13       phx 			return 1;
    187  1.13       phx 	} else {
    188  1.13       phx 		/* indirect config - check typical address */
    189  1.13       phx 		if (ia->ia_addr == DS1307_ADDR)
    190  1.15      matt 			return dsrtc_model(cf->cf_flags & 0xffff) != NULL;
    191  1.13       phx 	}
    192  1.13       phx 	return 0;
    193   1.1   thorpej }
    194   1.1   thorpej 
    195   1.1   thorpej static void
    196  1.11   xtraeme dsrtc_attach(device_t parent, device_t self, void *arg)
    197   1.1   thorpej {
    198   1.5   thorpej 	struct dsrtc_softc *sc = device_private(self);
    199   1.1   thorpej 	struct i2c_attach_args *ia = arg;
    200  1.15      matt 	const struct dsrtc_model * const dm =
    201  1.15      matt 	    dsrtc_model(device_cfdata(self)->cf_flags);
    202   1.1   thorpej 
    203  1.15      matt 	aprint_naive(": Real-time Clock%s\n",
    204  1.15      matt 	    dm->dm_nvram_size > 0 ? "/NVRAM" : "");
    205  1.15      matt 	aprint_normal(": DS%u Real-time Clock%s\n", dm->dm_model,
    206  1.15      matt 	    dm->dm_nvram_size > 0 ? "/NVRAM" : "");
    207   1.1   thorpej 
    208   1.1   thorpej 	sc->sc_tag = ia->ia_tag;
    209   1.1   thorpej 	sc->sc_address = ia->ia_addr;
    210  1.15      matt 	sc->sc_model = *dm;
    211  1.11   xtraeme 	sc->sc_dev = self;
    212   1.1   thorpej 	sc->sc_open = 0;
    213   1.1   thorpej 	sc->sc_todr.cookie = sc;
    214  1.15      matt 	if (dm->dm_flags & DSRTC_FLAG_BCD) {
    215  1.15      matt 		sc->sc_todr.todr_gettime_ymdhms = dsrtc_gettime_ymdhms;
    216  1.15      matt 		sc->sc_todr.todr_settime_ymdhms = dsrtc_settime_ymdhms;
    217  1.15      matt 	} else {
    218  1.15      matt 		sc->sc_todr.todr_gettime = dsrtc_gettime_timeval;
    219  1.15      matt 		sc->sc_todr.todr_settime = dsrtc_settime_timeval;
    220  1.15      matt 	}
    221   1.1   thorpej 	sc->sc_todr.todr_setwen = NULL;
    222   1.1   thorpej 
    223   1.1   thorpej 	todr_attach(&sc->sc_todr);
    224  1.19  macallan 	if ((sc->sc_model.dm_flags & DSRTC_FLAG_TEMP) != 0) {
    225  1.19  macallan 		int error;
    226  1.19  macallan 
    227  1.19  macallan 		sc->sc_sme = sysmon_envsys_create();
    228  1.19  macallan 		sc->sc_sme->sme_name = device_xname(self);
    229  1.19  macallan 		sc->sc_sme->sme_cookie = sc;
    230  1.19  macallan 		sc->sc_sme->sme_refresh = dsrtc_refresh;
    231  1.19  macallan 
    232  1.19  macallan 		sc->sc_sensor.units =  ENVSYS_STEMP;
    233  1.19  macallan 		sc->sc_sensor.state = ENVSYS_SINVALID;
    234  1.19  macallan 		sc->sc_sensor.flags = 0;
    235  1.19  macallan 		(void)strlcpy(sc->sc_sensor.desc, "temperature",
    236  1.19  macallan 		    sizeof(sc->sc_sensor.desc));
    237  1.19  macallan 
    238  1.19  macallan 		if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor)) {
    239  1.19  macallan 			aprint_error_dev(self, "unable to attach sensor\n");
    240  1.19  macallan 			goto bad;
    241  1.19  macallan 		}
    242  1.19  macallan 
    243  1.19  macallan 		error = sysmon_envsys_register(sc->sc_sme);
    244  1.19  macallan 		if (error) {
    245  1.19  macallan 			aprint_error_dev(self,
    246  1.19  macallan 			    "error %d registering with sysmon\n", error);
    247  1.19  macallan 			goto bad;
    248  1.19  macallan 		}
    249  1.19  macallan 	}
    250  1.19  macallan 	return;
    251  1.19  macallan bad:
    252  1.19  macallan 	sysmon_envsys_destroy(sc->sc_sme);
    253   1.1   thorpej }
    254   1.1   thorpej 
    255   1.1   thorpej /*ARGSUSED*/
    256   1.1   thorpej int
    257   1.4       abs dsrtc_open(dev_t dev, int flag, int fmt, struct lwp *l)
    258   1.1   thorpej {
    259   1.1   thorpej 	struct dsrtc_softc *sc;
    260   1.1   thorpej 
    261  1.12   tsutsui 	if ((sc = device_lookup_private(&dsrtc_cd, minor(dev))) == NULL)
    262  1.14       phx 		return ENXIO;
    263   1.1   thorpej 
    264   1.1   thorpej 	/* XXX: Locking */
    265   1.1   thorpej 	if (sc->sc_open)
    266  1.14       phx 		return EBUSY;
    267   1.1   thorpej 
    268  1.15      matt 	sc->sc_open = true;
    269  1.14       phx 	return 0;
    270   1.1   thorpej }
    271   1.1   thorpej 
    272   1.1   thorpej /*ARGSUSED*/
    273   1.1   thorpej int
    274   1.4       abs dsrtc_close(dev_t dev, int flag, int fmt, struct lwp *l)
    275   1.1   thorpej {
    276   1.1   thorpej 	struct dsrtc_softc *sc;
    277   1.1   thorpej 
    278  1.12   tsutsui 	if ((sc = device_lookup_private(&dsrtc_cd, minor(dev))) == NULL)
    279  1.14       phx 		return ENXIO;
    280   1.1   thorpej 
    281  1.15      matt 	sc->sc_open = false;
    282  1.14       phx 	return 0;
    283   1.1   thorpej }
    284   1.1   thorpej 
    285   1.1   thorpej /*ARGSUSED*/
    286   1.1   thorpej int
    287   1.1   thorpej dsrtc_read(dev_t dev, struct uio *uio, int flags)
    288   1.1   thorpej {
    289   1.1   thorpej 	struct dsrtc_softc *sc;
    290  1.15      matt 	int error;
    291   1.1   thorpej 
    292  1.12   tsutsui 	if ((sc = device_lookup_private(&dsrtc_cd, minor(dev))) == NULL)
    293  1.14       phx 		return ENXIO;
    294   1.1   thorpej 
    295  1.15      matt 	const struct dsrtc_model * const dm = &sc->sc_model;
    296  1.15      matt 	if (uio->uio_offset >= dm->dm_nvram_size)
    297  1.14       phx 		return EINVAL;
    298   1.1   thorpej 
    299   1.1   thorpej 	if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0)
    300  1.14       phx 		return error;
    301   1.1   thorpej 
    302  1.15      matt 	KASSERT(uio->uio_offset >= 0);
    303  1.15      matt 	while (uio->uio_resid && uio->uio_offset < dm->dm_nvram_size) {
    304  1.15      matt 		uint8_t ch, cmd;
    305  1.15      matt 		const u_int a = uio->uio_offset;
    306  1.15      matt 		cmd = a + dm->dm_nvram_start;
    307  1.15      matt 		if ((error = iic_exec(sc->sc_tag,
    308  1.15      matt 		    uio->uio_resid > 1 ? I2C_OP_READ : I2C_OP_READ_WITH_STOP,
    309  1.15      matt 		    sc->sc_address, &cmd, 1, &ch, 1, 0)) != 0) {
    310   1.1   thorpej 			iic_release_bus(sc->sc_tag, 0);
    311  1.11   xtraeme 			aprint_error_dev(sc->sc_dev,
    312  1.16      matt 			    "%s: read failed at 0x%x: %d\n",
    313  1.16      matt 			    __func__, a, error);
    314  1.14       phx 			return error;
    315   1.1   thorpej 		}
    316   1.1   thorpej 		if ((error = uiomove(&ch, 1, uio)) != 0) {
    317   1.1   thorpej 			iic_release_bus(sc->sc_tag, 0);
    318  1.14       phx 			return error;
    319   1.1   thorpej 		}
    320   1.1   thorpej 	}
    321   1.1   thorpej 
    322   1.1   thorpej 	iic_release_bus(sc->sc_tag, 0);
    323   1.1   thorpej 
    324  1.14       phx 	return 0;
    325   1.1   thorpej }
    326   1.1   thorpej 
    327   1.1   thorpej /*ARGSUSED*/
    328   1.1   thorpej int
    329   1.1   thorpej dsrtc_write(dev_t dev, struct uio *uio, int flags)
    330   1.1   thorpej {
    331   1.1   thorpej 	struct dsrtc_softc *sc;
    332  1.15      matt 	int error;
    333   1.1   thorpej 
    334  1.12   tsutsui 	if ((sc = device_lookup_private(&dsrtc_cd, minor(dev))) == NULL)
    335  1.14       phx 		return ENXIO;
    336   1.1   thorpej 
    337  1.15      matt 	const struct dsrtc_model * const dm = &sc->sc_model;
    338  1.15      matt 	if (uio->uio_offset >= dm->dm_nvram_size)
    339  1.14       phx 		return EINVAL;
    340   1.1   thorpej 
    341   1.1   thorpej 	if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0)
    342  1.14       phx 		return error;
    343   1.1   thorpej 
    344  1.15      matt 	while (uio->uio_resid && uio->uio_offset < dm->dm_nvram_size) {
    345  1.15      matt 		uint8_t cmdbuf[2];
    346  1.15      matt 		const u_int a = (int)uio->uio_offset;
    347  1.15      matt 		cmdbuf[0] = a + dm->dm_nvram_start;
    348   1.1   thorpej 		if ((error = uiomove(&cmdbuf[1], 1, uio)) != 0)
    349   1.1   thorpej 			break;
    350   1.1   thorpej 
    351   1.1   thorpej 		if ((error = iic_exec(sc->sc_tag,
    352   1.1   thorpej 		    uio->uio_resid ? I2C_OP_WRITE : I2C_OP_WRITE_WITH_STOP,
    353   1.1   thorpej 		    sc->sc_address, cmdbuf, 1, &cmdbuf[1], 1, 0)) != 0) {
    354  1.11   xtraeme 			aprint_error_dev(sc->sc_dev,
    355  1.16      matt 			    "%s: write failed at 0x%x: %d\n",
    356  1.16      matt 			    __func__, a, error);
    357   1.1   thorpej 			break;
    358   1.1   thorpej 		}
    359   1.1   thorpej 	}
    360   1.1   thorpej 
    361   1.1   thorpej 	iic_release_bus(sc->sc_tag, 0);
    362   1.1   thorpej 
    363  1.14       phx 	return error;
    364   1.1   thorpej }
    365   1.1   thorpej 
    366   1.1   thorpej static int
    367  1.15      matt dsrtc_gettime_ymdhms(struct todr_chip_handle *ch, struct clock_ymdhms *dt)
    368   1.1   thorpej {
    369   1.1   thorpej 	struct dsrtc_softc *sc = ch->cookie;
    370   1.7   gdamore 	struct clock_ymdhms check;
    371   1.1   thorpej 	int retries;
    372   1.1   thorpej 
    373   1.7   gdamore 	memset(dt, 0, sizeof(*dt));
    374   1.1   thorpej 	memset(&check, 0, sizeof(check));
    375   1.1   thorpej 
    376   1.1   thorpej 	/*
    377   1.1   thorpej 	 * Since we don't support Burst Read, we have to read the clock twice
    378   1.1   thorpej 	 * until we get two consecutive identical results.
    379   1.1   thorpej 	 */
    380   1.1   thorpej 	retries = 5;
    381   1.1   thorpej 	do {
    382  1.15      matt 		dsrtc_clock_read_ymdhms(sc, dt);
    383  1.15      matt 		dsrtc_clock_read_ymdhms(sc, &check);
    384   1.7   gdamore 	} while (memcmp(dt, &check, sizeof(check)) != 0 && --retries);
    385   1.1   thorpej 
    386  1.14       phx 	return 0;
    387   1.1   thorpej }
    388   1.1   thorpej 
    389   1.1   thorpej static int
    390  1.15      matt dsrtc_settime_ymdhms(struct todr_chip_handle *ch, struct clock_ymdhms *dt)
    391   1.1   thorpej {
    392   1.1   thorpej 	struct dsrtc_softc *sc = ch->cookie;
    393   1.1   thorpej 
    394  1.15      matt 	if (dsrtc_clock_write_ymdhms(sc, dt) == 0)
    395  1.14       phx 		return -1;
    396   1.1   thorpej 
    397  1.14       phx 	return 0;
    398   1.1   thorpej }
    399   1.1   thorpej 
    400   1.1   thorpej static int
    401  1.15      matt dsrtc_clock_read_ymdhms(struct dsrtc_softc *sc, struct clock_ymdhms *dt)
    402   1.1   thorpej {
    403  1.15      matt 	struct dsrtc_model * const dm = &sc->sc_model;
    404  1.15      matt 	uint8_t bcd[DSXXXX_RTC_SIZE], cmdbuf[1];
    405  1.16      matt 	int error;
    406  1.15      matt 
    407  1.15      matt 	KASSERT(DSXXXX_RTC_SIZE >= dm->dm_rtc_size);
    408   1.1   thorpej 
    409  1.16      matt 	if ((error = iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) != 0) {
    410  1.11   xtraeme 		aprint_error_dev(sc->sc_dev,
    411  1.16      matt 		    "%s: failed to acquire I2C bus: %d\n",
    412  1.16      matt 		    __func__, error);
    413  1.14       phx 		return 0;
    414   1.1   thorpej 	}
    415   1.1   thorpej 
    416   1.1   thorpej 	/* Read each RTC register in order. */
    417  1.16      matt 	for (u_int i = 0; !error && i < dm->dm_rtc_size; i++) {
    418  1.15      matt 		cmdbuf[0] = dm->dm_rtc_start + i;
    419   1.1   thorpej 
    420  1.16      matt 		error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
    421  1.16      matt 		    sc->sc_address, cmdbuf, 1, &bcd[i], 1, I2C_F_POLL);
    422   1.1   thorpej 	}
    423   1.1   thorpej 
    424   1.1   thorpej 	/* Done with I2C */
    425   1.1   thorpej 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    426   1.1   thorpej 
    427  1.16      matt 	if (error != 0) {
    428  1.16      matt 		aprint_error_dev(sc->sc_dev,
    429  1.16      matt 		    "%s: failed to read rtc at 0x%x: %d\n",
    430  1.16      matt 		    __func__, cmdbuf[0], error);
    431  1.16      matt 		return 0;
    432  1.16      matt 	}
    433  1.16      matt 
    434   1.1   thorpej 	/*
    435  1.15      matt 	 * Convert the RTC's register values into something useable
    436   1.1   thorpej 	 */
    437  1.21  christos 	dt->dt_sec = bcdtobin(bcd[DSXXXX_SECONDS] & DSXXXX_SECONDS_MASK);
    438  1.21  christos 	dt->dt_min = bcdtobin(bcd[DSXXXX_MINUTES] & DSXXXX_MINUTES_MASK);
    439   1.1   thorpej 
    440  1.15      matt 	if ((bcd[DSXXXX_HOURS] & DSXXXX_HOURS_12HRS_MODE) != 0) {
    441  1.21  christos 		dt->dt_hour = bcdtobin(bcd[DSXXXX_HOURS] &
    442  1.15      matt 		    DSXXXX_HOURS_12MASK) % 12; /* 12AM -> 0, 12PM -> 12 */
    443  1.15      matt 		if (bcd[DSXXXX_HOURS] & DSXXXX_HOURS_12HRS_PM)
    444   1.1   thorpej 			dt->dt_hour += 12;
    445  1.14       phx 	} else
    446  1.21  christos 		dt->dt_hour = bcdtobin(bcd[DSXXXX_HOURS] &
    447  1.15      matt 		    DSXXXX_HOURS_24MASK);
    448   1.1   thorpej 
    449  1.21  christos 	dt->dt_day = bcdtobin(bcd[DSXXXX_DATE] & DSXXXX_DATE_MASK);
    450  1.21  christos 	dt->dt_mon = bcdtobin(bcd[DSXXXX_MONTH] & DSXXXX_MONTH_MASK);
    451   1.1   thorpej 
    452   1.1   thorpej 	/* XXX: Should be an MD way to specify EPOCH used by BIOS/Firmware */
    453  1.21  christos 	dt->dt_year = bcdtobin(bcd[DSXXXX_YEAR]) + POSIX_BASE_YEAR;
    454  1.15      matt 	if (bcd[DSXXXX_MONTH] & DSXXXX_MONTH_CENTURY)
    455  1.15      matt 		dt->dt_year += 100;
    456   1.1   thorpej 
    457  1.14       phx 	return 1;
    458   1.1   thorpej }
    459   1.1   thorpej 
    460   1.1   thorpej static int
    461  1.15      matt dsrtc_clock_write_ymdhms(struct dsrtc_softc *sc, struct clock_ymdhms *dt)
    462   1.1   thorpej {
    463  1.15      matt 	struct dsrtc_model * const dm = &sc->sc_model;
    464  1.15      matt 	uint8_t bcd[DSXXXX_RTC_SIZE], cmdbuf[2];
    465  1.16      matt 	int error;
    466  1.15      matt 
    467  1.15      matt 	KASSERT(DSXXXX_RTC_SIZE >= dm->dm_rtc_size);
    468   1.1   thorpej 
    469   1.1   thorpej 	/*
    470  1.15      matt 	 * Convert our time representation into something the DSXXXX
    471   1.1   thorpej 	 * can understand.
    472   1.1   thorpej 	 */
    473  1.21  christos 	bcd[DSXXXX_SECONDS] = bintobcd(dt->dt_sec);
    474  1.21  christos 	bcd[DSXXXX_MINUTES] = bintobcd(dt->dt_min);
    475  1.21  christos 	bcd[DSXXXX_HOURS] = bintobcd(dt->dt_hour); /* DSXXXX_HOURS_12HRS_MODE=0 */
    476  1.21  christos 	bcd[DSXXXX_DATE] = bintobcd(dt->dt_day);
    477  1.21  christos 	bcd[DSXXXX_DAY] = bintobcd(dt->dt_wday);
    478  1.21  christos 	bcd[DSXXXX_MONTH] = bintobcd(dt->dt_mon);
    479  1.21  christos 	bcd[DSXXXX_YEAR] = bintobcd((dt->dt_year - POSIX_BASE_YEAR) % 100);
    480  1.15      matt 	if (dt->dt_year - POSIX_BASE_YEAR >= 100)
    481  1.15      matt 		bcd[DSXXXX_MONTH] |= DSXXXX_MONTH_CENTURY;
    482   1.1   thorpej 
    483  1.16      matt 	if ((error = iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) != 0) {
    484  1.11   xtraeme 		aprint_error_dev(sc->sc_dev,
    485  1.16      matt 		    "%s: failed to acquire I2C bus: %d\n",
    486  1.16      matt 		    __func__, error);
    487  1.14       phx 		return 0;
    488   1.1   thorpej 	}
    489   1.1   thorpej 
    490   1.1   thorpej 	/* Stop the clock */
    491  1.15      matt 	cmdbuf[0] = dm->dm_ch_reg;
    492  1.15      matt 
    493  1.16      matt 	if ((error = iic_exec(sc->sc_tag, I2C_OP_READ, sc->sc_address,
    494  1.16      matt 	    cmdbuf, 1, &cmdbuf[1], 1, I2C_F_POLL)) != 0) {
    495  1.15      matt 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
    496  1.15      matt 		aprint_error_dev(sc->sc_dev,
    497  1.16      matt 		    "%s: failed to read Hold Clock: %d\n",
    498  1.16      matt 		    __func__, error);
    499  1.15      matt 		return 0;
    500  1.15      matt 	}
    501  1.15      matt 
    502  1.15      matt 	cmdbuf[1] |= dm->dm_ch_value;
    503   1.1   thorpej 
    504  1.16      matt 	if ((error = iic_exec(sc->sc_tag, I2C_OP_WRITE, sc->sc_address,
    505  1.16      matt 	    cmdbuf, 1, &cmdbuf[1], 1, I2C_F_POLL)) != 0) {
    506   1.1   thorpej 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
    507  1.11   xtraeme 		aprint_error_dev(sc->sc_dev,
    508  1.16      matt 		    "%s: failed to write Hold Clock: %d\n",
    509  1.16      matt 		    __func__, error);
    510  1.14       phx 		return 0;
    511   1.1   thorpej 	}
    512   1.1   thorpej 
    513   1.1   thorpej 	/*
    514   1.1   thorpej 	 * Write registers in reverse order. The last write (to the Seconds
    515   1.1   thorpej 	 * register) will undo the Clock Hold, above.
    516   1.1   thorpej 	 */
    517  1.15      matt 	uint8_t op = I2C_OP_WRITE;
    518  1.15      matt 	for (signed int i = dm->dm_rtc_size - 1; i >= 0; i--) {
    519  1.15      matt 		cmdbuf[0] = dm->dm_rtc_start + i;
    520  1.15      matt 		if (dm->dm_rtc_start + i == dm->dm_ch_reg) {
    521  1.15      matt 			op = I2C_OP_WRITE_WITH_STOP;
    522  1.15      matt 		}
    523  1.16      matt 		if ((error = iic_exec(sc->sc_tag, op, sc->sc_address,
    524  1.16      matt 		    cmdbuf, 1, &bcd[i], 1, I2C_F_POLL)) != 0) {
    525   1.1   thorpej 			iic_release_bus(sc->sc_tag, I2C_F_POLL);
    526  1.11   xtraeme 			aprint_error_dev(sc->sc_dev,
    527  1.16      matt 			    "%s: failed to write rtc at 0x%x: %d\n",
    528  1.16      matt 			    __func__, i, error);
    529   1.1   thorpej 			/* XXX: Clock Hold is likely still asserted! */
    530  1.14       phx 			return 0;
    531   1.1   thorpej 		}
    532   1.1   thorpej 	}
    533  1.15      matt 	/*
    534  1.15      matt 	 * If the clock hold register isn't the same register as seconds,
    535  1.15      matt 	 * we need to reeanble the clock.
    536  1.15      matt 	 */
    537  1.15      matt 	if (op != I2C_OP_WRITE_WITH_STOP) {
    538  1.15      matt 		cmdbuf[0] = dm->dm_ch_reg;
    539  1.15      matt 		cmdbuf[1] &= ~dm->dm_ch_value;
    540  1.15      matt 
    541  1.16      matt 		if ((error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
    542  1.16      matt 		    sc->sc_address, cmdbuf, 1, &cmdbuf[1], 1,
    543  1.16      matt 		    I2C_F_POLL)) != 0) {
    544  1.15      matt 			iic_release_bus(sc->sc_tag, I2C_F_POLL);
    545  1.15      matt 			aprint_error_dev(sc->sc_dev,
    546  1.16      matt 			    "%s: failed to Hold Clock: %d\n",
    547  1.16      matt 			    __func__, error);
    548  1.15      matt 			return 0;
    549  1.15      matt 		}
    550  1.15      matt 	}
    551   1.1   thorpej 
    552   1.1   thorpej 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    553   1.1   thorpej 
    554  1.14       phx 	return 1;
    555   1.1   thorpej }
    556  1.15      matt 
    557  1.15      matt static int
    558  1.15      matt dsrtc_gettime_timeval(struct todr_chip_handle *ch, struct timeval *tv)
    559  1.15      matt {
    560  1.15      matt 	struct dsrtc_softc *sc = ch->cookie;
    561  1.15      matt 	struct timeval check;
    562  1.15      matt 	int retries;
    563  1.15      matt 
    564  1.15      matt 	memset(tv, 0, sizeof(*tv));
    565  1.15      matt 	memset(&check, 0, sizeof(check));
    566  1.15      matt 
    567  1.15      matt 	/*
    568  1.15      matt 	 * Since we don't support Burst Read, we have to read the clock twice
    569  1.15      matt 	 * until we get two consecutive identical results.
    570  1.15      matt 	 */
    571  1.15      matt 	retries = 5;
    572  1.15      matt 	do {
    573  1.15      matt 		dsrtc_clock_read_timeval(sc, &tv->tv_sec);
    574  1.15      matt 		dsrtc_clock_read_timeval(sc, &check.tv_sec);
    575  1.15      matt 	} while (memcmp(tv, &check, sizeof(check)) != 0 && --retries);
    576  1.15      matt 
    577  1.15      matt 	return 0;
    578  1.15      matt }
    579  1.15      matt 
    580  1.15      matt static int
    581  1.15      matt dsrtc_settime_timeval(struct todr_chip_handle *ch, struct timeval *tv)
    582  1.15      matt {
    583  1.15      matt 	struct dsrtc_softc *sc = ch->cookie;
    584  1.15      matt 
    585  1.15      matt 	if (dsrtc_clock_write_timeval(sc, tv->tv_sec) == 0)
    586  1.15      matt 		return -1;
    587  1.15      matt 
    588  1.15      matt 	return 0;
    589  1.15      matt }
    590  1.15      matt 
    591  1.15      matt /*
    592  1.15      matt  * The RTC probably has a nice Clock Burst Read/Write command, but we can't use
    593  1.15      matt  * it, since some I2C controllers don't support anything other than single-byte
    594  1.15      matt  * transfers.
    595  1.15      matt  */
    596  1.15      matt static int
    597  1.15      matt dsrtc_clock_read_timeval(struct dsrtc_softc *sc, time_t *tp)
    598  1.15      matt {
    599  1.15      matt 	const struct dsrtc_model * const dm = &sc->sc_model;
    600  1.15      matt 	uint8_t buf[4];
    601  1.16      matt 	int error;
    602  1.15      matt 
    603  1.16      matt 	if ((error = iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) != 0) {
    604  1.16      matt 		aprint_error_dev(sc->sc_dev,
    605  1.16      matt 		    "%s: failed to acquire I2C bus: %d\n",
    606  1.16      matt 		    __func__, error);
    607  1.16      matt 		return 0;
    608  1.15      matt 	}
    609  1.15      matt 
    610  1.15      matt 	/* read all registers: */
    611  1.15      matt 	uint8_t reg = dm->dm_rtc_start;
    612  1.16      matt 	error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_address,
    613  1.16      matt 	     &reg, 1, buf, 4, I2C_F_POLL);
    614  1.15      matt 
    615  1.15      matt 	/* Done with I2C */
    616  1.15      matt 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    617  1.15      matt 
    618  1.16      matt 	if (error != 0) {
    619  1.16      matt 		aprint_error_dev(sc->sc_dev,
    620  1.16      matt 		    "%s: failed to read rtc at 0x%x: %d\n",
    621  1.16      matt 		    __func__, reg, error);
    622  1.16      matt 		return 0;
    623  1.16      matt 	}
    624  1.16      matt 
    625  1.15      matt 	uint32_t v = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
    626  1.15      matt 	*tp = v;
    627  1.15      matt 
    628  1.15      matt 	aprint_debug_dev(sc->sc_dev, "%s: cntr=0x%08"PRIx32"\n",
    629  1.15      matt 	    __func__, v);
    630  1.15      matt 
    631  1.16      matt 	return 1;
    632  1.15      matt }
    633  1.15      matt 
    634  1.15      matt static int
    635  1.15      matt dsrtc_clock_write_timeval(struct dsrtc_softc *sc, time_t t)
    636  1.15      matt {
    637  1.15      matt 	const struct dsrtc_model * const dm = &sc->sc_model;
    638  1.15      matt 	size_t buflen = dm->dm_rtc_size + 2;
    639  1.15      matt 	uint8_t buf[buflen];
    640  1.16      matt 	int error;
    641  1.15      matt 
    642  1.15      matt 	KASSERT((dm->dm_flags & DSRTC_FLAG_CLOCK_HOLD) == 0);
    643  1.15      matt 	KASSERT(dm->dm_ch_reg == dm->dm_rtc_start + 4);
    644  1.15      matt 
    645  1.15      matt 	buf[0] = dm->dm_rtc_start;
    646  1.15      matt 	buf[1] = (t >> 0) & 0xff;
    647  1.15      matt 	buf[2] = (t >> 8) & 0xff;
    648  1.15      matt 	buf[3] = (t >> 16) & 0xff;
    649  1.15      matt 	buf[4] = (t >> 24) & 0xff;
    650  1.15      matt 	buf[5] = 0;
    651  1.15      matt 
    652  1.16      matt 	if ((error = iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) != 0) {
    653  1.16      matt 		aprint_error_dev(sc->sc_dev,
    654  1.16      matt 		    "%s: failed to acquire I2C bus: %d\n",
    655  1.16      matt 		    __func__, error);
    656  1.16      matt 		return 0;
    657  1.15      matt 	}
    658  1.15      matt 
    659  1.16      matt 	error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address,
    660  1.16      matt 	    &buf, buflen, NULL, 0, I2C_F_POLL);
    661  1.16      matt 
    662  1.16      matt 	/* Done with I2C */
    663  1.16      matt 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    664  1.16      matt 
    665  1.15      matt 	/* send data */
    666  1.16      matt 	if (error != 0) {
    667  1.16      matt 		aprint_error_dev(sc->sc_dev,
    668  1.16      matt 		    "%s: failed to set time: %d\n",
    669  1.16      matt 		    __func__, error);
    670  1.16      matt 		return 0;
    671  1.15      matt 	}
    672  1.15      matt 
    673  1.16      matt 	return 1;
    674  1.15      matt }
    675  1.19  macallan 
    676  1.19  macallan static int
    677  1.19  macallan dsrtc_read_temp(struct dsrtc_softc *sc, uint32_t *temp)
    678  1.19  macallan {
    679  1.19  macallan 	int error, tc;
    680  1.19  macallan 	uint8_t reg = DS3232_TEMP_MSB;
    681  1.19  macallan 	uint8_t buf[2];
    682  1.19  macallan 
    683  1.19  macallan 	if ((sc->sc_model.dm_flags & DSRTC_FLAG_TEMP) == 0)
    684  1.19  macallan 		return ENOTSUP;
    685  1.19  macallan 
    686  1.19  macallan 	if ((error = iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) != 0) {
    687  1.19  macallan 		aprint_error_dev(sc->sc_dev,
    688  1.19  macallan 		    "%s: failed to acquire I2C bus: %d\n",
    689  1.19  macallan 		    __func__, error);
    690  1.19  macallan 		return 0;
    691  1.19  macallan 	}
    692  1.19  macallan 
    693  1.19  macallan 	/* read temperature registers: */
    694  1.19  macallan 	error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_address,
    695  1.19  macallan 	     &reg, 1, buf, 2, I2C_F_POLL);
    696  1.19  macallan 
    697  1.19  macallan 	/* Done with I2C */
    698  1.19  macallan 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    699  1.19  macallan 
    700  1.19  macallan 	if (error != 0) {
    701  1.19  macallan 		aprint_error_dev(sc->sc_dev,
    702  1.19  macallan 		    "%s: failed to read temperature: %d\n",
    703  1.19  macallan 		    __func__, error);
    704  1.19  macallan 		return 0;
    705  1.19  macallan 	}
    706  1.19  macallan 
    707  1.19  macallan 	/* convert to microkelvin */
    708  1.19  macallan 	tc = buf[0] * 1000000 + (buf[1] >> 6) * 250000;
    709  1.19  macallan 	*temp = tc + 273150000;
    710  1.19  macallan 	return 1;
    711  1.19  macallan }
    712  1.19  macallan 
    713  1.19  macallan static void
    714  1.19  macallan dsrtc_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    715  1.19  macallan {
    716  1.19  macallan 	struct dsrtc_softc *sc = sme->sme_cookie;
    717  1.20    martin 	uint32_t temp = 0;	/* XXX gcc */
    718  1.19  macallan 
    719  1.19  macallan 	if (dsrtc_read_temp(sc, &temp) == 0) {
    720  1.19  macallan 		edata->state = ENVSYS_SINVALID;
    721  1.19  macallan 		return;
    722  1.19  macallan 	}
    723  1.19  macallan 
    724  1.19  macallan 	edata->value_cur = temp;
    725  1.19  macallan 
    726  1.19  macallan 	edata->state = ENVSYS_SVALID;
    727  1.19  macallan }
    728