Home | History | Annotate | Line # | Download | only in i2c
s390.c revision 1.4
      1  1.4   thorpej /*	$NetBSD: s390.c,v 1.4 2018/06/16 21:22:13 thorpej Exp $	*/
      2  1.1       phx 
      3  1.1       phx /*-
      4  1.1       phx  * Copyright (c) 2011 Frank Wille.
      5  1.1       phx  * All rights reserved.
      6  1.1       phx  *
      7  1.1       phx  * Written by Frank Wille for The NetBSD Project.
      8  1.1       phx  *
      9  1.1       phx  * Redistribution and use in source and binary forms, with or without
     10  1.1       phx  * modification, are permitted provided that the following conditions
     11  1.1       phx  * are met:
     12  1.1       phx  * 1. Redistributions of source code must retain the above copyright
     13  1.1       phx  *    notice, this list of conditions and the following disclaimer.
     14  1.1       phx  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1       phx  *    notice, this list of conditions and the following disclaimer in the
     16  1.1       phx  *    documentation and/or other materials provided with the distribution.
     17  1.1       phx  *
     18  1.1       phx  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19  1.1       phx  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20  1.1       phx  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21  1.1       phx  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22  1.1       phx  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  1.1       phx  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  1.1       phx  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  1.1       phx  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  1.1       phx  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  1.1       phx  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  1.1       phx  * POSSIBILITY OF SUCH DAMAGE.
     29  1.1       phx  */
     30  1.1       phx 
     31  1.1       phx #include <sys/cdefs.h>
     32  1.4   thorpej __KERNEL_RCSID(0, "$NetBSD: s390.c,v 1.4 2018/06/16 21:22:13 thorpej Exp $");
     33  1.1       phx 
     34  1.1       phx #include <sys/param.h>
     35  1.1       phx #include <sys/systm.h>
     36  1.1       phx #include <sys/device.h>
     37  1.1       phx #include <sys/conf.h>
     38  1.1       phx 
     39  1.1       phx #include <dev/clock_subr.h>
     40  1.1       phx 
     41  1.1       phx #include <dev/i2c/i2cvar.h>
     42  1.1       phx #include <dev/i2c/s390reg.h>
     43  1.1       phx 
     44  1.1       phx struct s390rtc_softc {
     45  1.1       phx 	device_t sc_dev;
     46  1.1       phx 	i2c_tag_t sc_tag;
     47  1.1       phx 	i2c_addr_t sc_addr;
     48  1.1       phx 	struct todr_chip_handle sc_todr;
     49  1.1       phx };
     50  1.1       phx 
     51  1.1       phx static int s390rtc_match(device_t, cfdata_t, void *);
     52  1.1       phx static void s390rtc_attach(device_t, device_t, void *);
     53  1.1       phx 
     54  1.1       phx CFATTACH_DECL_NEW(s390rtc, sizeof(struct s390rtc_softc),
     55  1.1       phx     s390rtc_match, s390rtc_attach, NULL, NULL);
     56  1.1       phx 
     57  1.1       phx static int s390rtc_gettime(struct todr_chip_handle *, struct timeval *);
     58  1.1       phx static int s390rtc_settime(struct todr_chip_handle *, struct timeval *);
     59  1.1       phx static int s390rtc_clock_read(struct s390rtc_softc *, struct clock_ymdhms *);
     60  1.1       phx static int s390rtc_clock_write(struct s390rtc_softc *, struct clock_ymdhms *);
     61  1.1       phx static int s390rtc_read(struct s390rtc_softc *, int, uint8_t *, size_t);
     62  1.1       phx static int s390rtc_write(struct s390rtc_softc *, int, uint8_t *, size_t);
     63  1.1       phx static uint8_t bitreverse(uint8_t);
     64  1.1       phx 
     65  1.1       phx static int
     66  1.1       phx s390rtc_match(device_t parent, cfdata_t cf, void *arg)
     67  1.1       phx {
     68  1.1       phx 	struct i2c_attach_args *ia = arg;
     69  1.4   thorpej 	int match_result;
     70  1.4   thorpej 
     71  1.4   thorpej 	if (iic_use_direct_match(ia, cf, NULL, &match_result))
     72  1.4   thorpej 		return match_result;
     73  1.4   thorpej 
     74  1.4   thorpej 	/* indirect config - check typical address */
     75  1.4   thorpej 	if (ia->ia_addr == S390_ADDR)
     76  1.4   thorpej 		return I2C_MATCH_ADDRESS_ONLY;
     77  1.1       phx 
     78  1.1       phx 	return 0;
     79  1.1       phx }
     80  1.1       phx 
     81  1.1       phx static void
     82  1.1       phx s390rtc_attach(device_t parent, device_t self, void *arg)
     83  1.1       phx {
     84  1.1       phx 	struct s390rtc_softc *sc = device_private(self);
     85  1.1       phx 	struct i2c_attach_args *ia = arg;
     86  1.1       phx 	uint8_t reg[1];
     87  1.1       phx 
     88  1.1       phx 	aprint_naive(": Real-time Clock\n");
     89  1.1       phx 	aprint_normal(": Seiko Instruments 35390A Real-time Clock\n");
     90  1.1       phx 
     91  1.1       phx 	sc->sc_tag = ia->ia_tag;
     92  1.1       phx 	sc->sc_addr = ia->ia_addr;
     93  1.1       phx 	sc->sc_dev = self;
     94  1.1       phx 
     95  1.1       phx 	/* Reset the chip and turn on 24h mode, after power-off or battery. */
     96  1.1       phx 	if (!s390rtc_read(sc, S390_STATUS1, reg, sizeof(reg)))
     97  1.1       phx 		return;
     98  1.1       phx 	if (reg[0] & (S390_ST1_POC | S390_ST1_BLD)) {
     99  1.1       phx 		reg[0] |= S390_ST1_24H | S390_ST1_RESET;
    100  1.1       phx 		if (!s390rtc_write(sc, S390_STATUS1, reg, sizeof(reg)))
    101  1.1       phx 			return;
    102  1.1       phx 	}
    103  1.1       phx 
    104  1.1       phx 	/* Disable the test mode, when enabled. */
    105  1.1       phx 	if (!s390rtc_read(sc, S390_STATUS2, reg, sizeof(reg)))
    106  1.1       phx 		return;
    107  1.1       phx 	if ((reg[0] & S390_ST2_TEST)) {
    108  1.1       phx 		reg[0] &= ~S390_ST2_TEST;
    109  1.1       phx 		if (!s390rtc_write(sc, S390_STATUS2, reg, sizeof(reg)))
    110  1.1       phx 			return;
    111  1.1       phx 	}
    112  1.1       phx 
    113  1.1       phx 	sc->sc_todr.cookie = sc;
    114  1.1       phx 	sc->sc_todr.todr_gettime = s390rtc_gettime;
    115  1.1       phx 	sc->sc_todr.todr_settime = s390rtc_settime;
    116  1.1       phx 	sc->sc_todr.todr_setwen = NULL;
    117  1.1       phx 	todr_attach(&sc->sc_todr);
    118  1.1       phx }
    119  1.1       phx 
    120  1.1       phx static int
    121  1.1       phx s390rtc_gettime(struct todr_chip_handle *ch, struct timeval *tv)
    122  1.1       phx {
    123  1.1       phx 	struct s390rtc_softc *sc = ch->cookie;
    124  1.1       phx 	struct clock_ymdhms dt;
    125  1.1       phx 
    126  1.1       phx 	memset(&dt, 0, sizeof(dt));
    127  1.1       phx 
    128  1.1       phx 	if (!s390rtc_clock_read(sc, &dt))
    129  1.1       phx 		return -1;
    130  1.1       phx 
    131  1.1       phx 	tv->tv_sec = clock_ymdhms_to_secs(&dt);
    132  1.1       phx 	tv->tv_usec = 0;
    133  1.1       phx 
    134  1.1       phx 	return 0;
    135  1.1       phx }
    136  1.1       phx 
    137  1.1       phx static int
    138  1.1       phx s390rtc_settime(struct todr_chip_handle *ch, struct timeval *tv)
    139  1.1       phx {
    140  1.1       phx 	struct s390rtc_softc *sc = ch->cookie;
    141  1.1       phx 	struct clock_ymdhms dt;
    142  1.1       phx 
    143  1.1       phx 	clock_secs_to_ymdhms(tv->tv_sec, &dt);
    144  1.1       phx 
    145  1.1       phx 	if (!s390rtc_clock_write(sc, &dt))
    146  1.1       phx 		return -1;
    147  1.1       phx 
    148  1.1       phx 	return 0;
    149  1.1       phx }
    150  1.1       phx 
    151  1.1       phx static int
    152  1.1       phx s390rtc_clock_read(struct s390rtc_softc *sc, struct clock_ymdhms *dt)
    153  1.1       phx {
    154  1.1       phx 	uint8_t bcd[S390_RT1_NBYTES];
    155  1.1       phx 
    156  1.1       phx 	if (!s390rtc_read(sc, S390_REALTIME1, bcd, S390_RT1_NBYTES))
    157  1.1       phx 		return 0;
    158  1.1       phx 
    159  1.1       phx 	/*
    160  1.1       phx 	 * Convert the register values into something useable.
    161  1.1       phx 	 */
    162  1.3  christos 	dt->dt_sec = bcdtobin(bcd[S390_RT1_SECOND]);
    163  1.3  christos 	dt->dt_min = bcdtobin(bcd[S390_RT1_MINUTE]);
    164  1.3  christos 	dt->dt_hour = bcdtobin(bcd[S390_RT1_HOUR] & 0x3f);
    165  1.3  christos 	dt->dt_day = bcdtobin(bcd[S390_RT1_DAY]);
    166  1.3  christos 	dt->dt_mon = bcdtobin(bcd[S390_RT1_MONTH]);
    167  1.3  christos 	dt->dt_year = bcdtobin(bcd[S390_RT1_YEAR]) + 2000;
    168  1.1       phx 
    169  1.1       phx 	return 1;
    170  1.1       phx }
    171  1.1       phx 
    172  1.1       phx static int
    173  1.1       phx s390rtc_clock_write(struct s390rtc_softc *sc, struct clock_ymdhms *dt)
    174  1.1       phx {
    175  1.1       phx 	uint8_t bcd[S390_RT1_NBYTES];
    176  1.1       phx 
    177  1.1       phx 	/*
    178  1.1       phx 	 * Convert our time representation into something the S-xx390
    179  1.1       phx 	 * can understand.
    180  1.1       phx 	 */
    181  1.3  christos 	bcd[S390_RT1_SECOND] = bintobcd(dt->dt_sec);
    182  1.3  christos 	bcd[S390_RT1_MINUTE] = bintobcd(dt->dt_min);
    183  1.3  christos 	bcd[S390_RT1_HOUR] = bintobcd(dt->dt_hour);
    184  1.3  christos 	bcd[S390_RT1_DAY] = bintobcd(dt->dt_day);
    185  1.3  christos 	bcd[S390_RT1_WDAY] = bintobcd(dt->dt_wday);
    186  1.3  christos 	bcd[S390_RT1_MONTH] = bintobcd(dt->dt_mon);
    187  1.3  christos 	bcd[S390_RT1_YEAR] = bintobcd(dt->dt_year % 100);
    188  1.1       phx 
    189  1.1       phx 	return s390rtc_write(sc, S390_REALTIME1, bcd, S390_RT1_NBYTES);
    190  1.1       phx }
    191  1.1       phx 
    192  1.1       phx static int
    193  1.1       phx s390rtc_read(struct s390rtc_softc *sc, int reg, uint8_t *buf, size_t len)
    194  1.1       phx {
    195  1.1       phx 	int i;
    196  1.1       phx 
    197  1.1       phx 	if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
    198  1.1       phx 		aprint_error_dev(sc->sc_dev,
    199  1.1       phx 		    "%s: failed to acquire I2C bus\n", __func__);
    200  1.1       phx 		return 0;
    201  1.1       phx 	}
    202  1.1       phx 
    203  1.1       phx 	if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr + reg,
    204  1.1       phx 	    NULL, 0, buf, len, I2C_F_POLL)) {
    205  1.1       phx 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
    206  1.1       phx 		aprint_error_dev(sc->sc_dev,
    207  1.1       phx 		    "%s: failed to read reg%d\n", __func__, reg);
    208  1.1       phx 		return 0;
    209  1.1       phx 	}
    210  1.1       phx 
    211  1.1       phx 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    212  1.1       phx 
    213  1.1       phx 	/* this chip returns each byte in reverse order */
    214  1.1       phx 	for (i = 0; i < len; i++)
    215  1.1       phx 		buf[i] = bitreverse(buf[i]);
    216  1.1       phx 
    217  1.1       phx 	return 1;
    218  1.1       phx }
    219  1.1       phx 
    220  1.1       phx static int
    221  1.1       phx s390rtc_write(struct s390rtc_softc *sc, int reg, uint8_t *buf, size_t len)
    222  1.1       phx {
    223  1.1       phx 	int i;
    224  1.1       phx 
    225  1.1       phx 	if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
    226  1.1       phx 		aprint_error_dev(sc->sc_dev,
    227  1.1       phx 		    "%s: failed to acquire I2C bus\n", __func__);
    228  1.1       phx 		return 0;
    229  1.1       phx 	}
    230  1.1       phx 
    231  1.1       phx 	/* this chip expects each byte in reverse order */
    232  1.1       phx 	for (i = 0; i < len; i++)
    233  1.1       phx 		buf[i] = bitreverse(buf[i]);
    234  1.1       phx 
    235  1.1       phx 	if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr + reg,
    236  1.1       phx 	    NULL, 0, buf, len, I2C_F_POLL)) {
    237  1.1       phx 		iic_release_bus(sc->sc_tag, I2C_F_POLL);
    238  1.1       phx 		aprint_error_dev(sc->sc_dev,
    239  1.1       phx 		    "%s: failed to write reg%d\n", __func__, reg);
    240  1.1       phx 		return 0;
    241  1.1       phx 	}
    242  1.1       phx 
    243  1.1       phx 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    244  1.1       phx 	return 1;
    245  1.1       phx }
    246  1.1       phx 
    247  1.1       phx static uint8_t
    248  1.1       phx bitreverse(uint8_t x)
    249  1.1       phx {
    250  1.1       phx 	static unsigned char nibbletab[16] = {
    251  1.1       phx 		0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
    252  1.1       phx 	};
    253  1.1       phx 
    254  1.1       phx 	return (nibbletab[x & 15] << 4) | nibbletab[x >> 4];
    255  1.1       phx }
    256