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