2021-01-30 20:38:57 +03:00
|
|
|
/* $NetBSD: s390.c,v 1.7 2021/01/30 17:38:57 thorpej Exp $ */
|
2011-04-04 21:58:40 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2011 Frank Wille.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Written by Frank Wille for The NetBSD Project.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2021-01-30 20:38:57 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: s390.c,v 1.7 2021/01/30 17:38:57 thorpej Exp $");
|
2011-04-04 21:58:40 +04:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/device.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
|
|
|
|
#include <dev/clock_subr.h>
|
|
|
|
|
|
|
|
#include <dev/i2c/i2cvar.h>
|
|
|
|
#include <dev/i2c/s390reg.h>
|
|
|
|
|
|
|
|
struct s390rtc_softc {
|
|
|
|
device_t sc_dev;
|
|
|
|
i2c_tag_t sc_tag;
|
|
|
|
i2c_addr_t sc_addr;
|
|
|
|
struct todr_chip_handle sc_todr;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int s390rtc_match(device_t, cfdata_t, void *);
|
|
|
|
static void s390rtc_attach(device_t, device_t, void *);
|
|
|
|
|
|
|
|
CFATTACH_DECL_NEW(s390rtc, sizeof(struct s390rtc_softc),
|
|
|
|
s390rtc_match, s390rtc_attach, NULL, NULL);
|
|
|
|
|
2020-01-02 22:00:34 +03:00
|
|
|
static int s390rtc_gettime_ymdhms(struct todr_chip_handle *,
|
|
|
|
struct clock_ymdhms *);
|
|
|
|
static int s390rtc_settime_ymdhms(struct todr_chip_handle *,
|
|
|
|
struct clock_ymdhms *);
|
2011-04-04 21:58:40 +04:00
|
|
|
static int s390rtc_read(struct s390rtc_softc *, int, uint8_t *, size_t);
|
|
|
|
static int s390rtc_write(struct s390rtc_softc *, int, uint8_t *, size_t);
|
|
|
|
static uint8_t bitreverse(uint8_t);
|
|
|
|
|
2021-01-30 20:38:57 +03:00
|
|
|
static const struct device_compatible_entry compat_data[] = {
|
|
|
|
{ .compat = "sii,s35390a" },
|
|
|
|
DEVICE_COMPAT_EOL
|
|
|
|
};
|
|
|
|
|
2011-04-04 21:58:40 +04:00
|
|
|
static int
|
|
|
|
s390rtc_match(device_t parent, cfdata_t cf, void *arg)
|
|
|
|
{
|
|
|
|
struct i2c_attach_args *ia = arg;
|
2018-06-17 00:22:13 +03:00
|
|
|
int match_result;
|
|
|
|
|
2021-01-30 20:38:57 +03:00
|
|
|
if (iic_use_direct_match(ia, cf, compat_data, &match_result))
|
2018-06-17 00:22:13 +03:00
|
|
|
return match_result;
|
|
|
|
|
|
|
|
/* indirect config - check typical address */
|
|
|
|
if (ia->ia_addr == S390_ADDR)
|
|
|
|
return I2C_MATCH_ADDRESS_ONLY;
|
2011-04-04 21:58:40 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
s390rtc_attach(device_t parent, device_t self, void *arg)
|
|
|
|
{
|
|
|
|
struct s390rtc_softc *sc = device_private(self);
|
|
|
|
struct i2c_attach_args *ia = arg;
|
|
|
|
uint8_t reg[1];
|
|
|
|
|
|
|
|
aprint_naive(": Real-time Clock\n");
|
|
|
|
aprint_normal(": Seiko Instruments 35390A Real-time Clock\n");
|
|
|
|
|
|
|
|
sc->sc_tag = ia->ia_tag;
|
|
|
|
sc->sc_addr = ia->ia_addr;
|
|
|
|
sc->sc_dev = self;
|
|
|
|
|
|
|
|
/* Reset the chip and turn on 24h mode, after power-off or battery. */
|
2020-01-02 20:17:36 +03:00
|
|
|
if (s390rtc_read(sc, S390_STATUS1, reg, sizeof(reg)))
|
2011-04-04 21:58:40 +04:00
|
|
|
return;
|
|
|
|
if (reg[0] & (S390_ST1_POC | S390_ST1_BLD)) {
|
|
|
|
reg[0] |= S390_ST1_24H | S390_ST1_RESET;
|
2020-01-02 20:17:36 +03:00
|
|
|
if (s390rtc_write(sc, S390_STATUS1, reg, sizeof(reg)))
|
2011-04-04 21:58:40 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Disable the test mode, when enabled. */
|
2020-01-02 20:17:36 +03:00
|
|
|
if (s390rtc_read(sc, S390_STATUS2, reg, sizeof(reg)))
|
2011-04-04 21:58:40 +04:00
|
|
|
return;
|
|
|
|
if ((reg[0] & S390_ST2_TEST)) {
|
|
|
|
reg[0] &= ~S390_ST2_TEST;
|
2020-01-02 20:17:36 +03:00
|
|
|
if (s390rtc_write(sc, S390_STATUS2, reg, sizeof(reg)))
|
2011-04-04 21:58:40 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sc->sc_todr.cookie = sc;
|
2020-01-02 22:00:34 +03:00
|
|
|
sc->sc_todr.todr_gettime = NULL;
|
|
|
|
sc->sc_todr.todr_settime = NULL;
|
|
|
|
sc->sc_todr.todr_gettime_ymdhms = s390rtc_gettime_ymdhms;
|
|
|
|
sc->sc_todr.todr_settime_ymdhms = s390rtc_settime_ymdhms;
|
2011-04-04 21:58:40 +04:00
|
|
|
sc->sc_todr.todr_setwen = NULL;
|
|
|
|
todr_attach(&sc->sc_todr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-01-02 22:00:34 +03:00
|
|
|
s390rtc_gettime_ymdhms(struct todr_chip_handle *ch, struct clock_ymdhms *dt)
|
2011-04-04 21:58:40 +04:00
|
|
|
{
|
|
|
|
struct s390rtc_softc *sc = ch->cookie;
|
|
|
|
uint8_t bcd[S390_RT1_NBYTES];
|
2020-01-02 20:17:36 +03:00
|
|
|
int error;
|
2011-04-04 21:58:40 +04:00
|
|
|
|
2020-01-02 20:17:36 +03:00
|
|
|
if ((error = s390rtc_read(sc, S390_REALTIME1, bcd,
|
|
|
|
S390_RT1_NBYTES)) != 0)
|
|
|
|
return error;
|
2011-04-04 21:58:40 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert the register values into something useable.
|
|
|
|
*/
|
2014-11-20 19:34:25 +03:00
|
|
|
dt->dt_sec = bcdtobin(bcd[S390_RT1_SECOND]);
|
|
|
|
dt->dt_min = bcdtobin(bcd[S390_RT1_MINUTE]);
|
|
|
|
dt->dt_hour = bcdtobin(bcd[S390_RT1_HOUR] & 0x3f);
|
|
|
|
dt->dt_day = bcdtobin(bcd[S390_RT1_DAY]);
|
|
|
|
dt->dt_mon = bcdtobin(bcd[S390_RT1_MONTH]);
|
|
|
|
dt->dt_year = bcdtobin(bcd[S390_RT1_YEAR]) + 2000;
|
2011-04-04 21:58:40 +04:00
|
|
|
|
2020-01-02 20:17:36 +03:00
|
|
|
return 0;
|
2011-04-04 21:58:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-01-02 22:00:34 +03:00
|
|
|
s390rtc_settime_ymdhms(struct todr_chip_handle *ch, struct clock_ymdhms *dt)
|
2011-04-04 21:58:40 +04:00
|
|
|
{
|
2020-01-02 22:00:34 +03:00
|
|
|
struct s390rtc_softc *sc = ch->cookie;
|
2011-04-04 21:58:40 +04:00
|
|
|
uint8_t bcd[S390_RT1_NBYTES];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert our time representation into something the S-xx390
|
|
|
|
* can understand.
|
|
|
|
*/
|
2014-11-20 19:34:25 +03:00
|
|
|
bcd[S390_RT1_SECOND] = bintobcd(dt->dt_sec);
|
|
|
|
bcd[S390_RT1_MINUTE] = bintobcd(dt->dt_min);
|
|
|
|
bcd[S390_RT1_HOUR] = bintobcd(dt->dt_hour);
|
|
|
|
bcd[S390_RT1_DAY] = bintobcd(dt->dt_day);
|
|
|
|
bcd[S390_RT1_WDAY] = bintobcd(dt->dt_wday);
|
|
|
|
bcd[S390_RT1_MONTH] = bintobcd(dt->dt_mon);
|
|
|
|
bcd[S390_RT1_YEAR] = bintobcd(dt->dt_year % 100);
|
2011-04-04 21:58:40 +04:00
|
|
|
|
|
|
|
return s390rtc_write(sc, S390_REALTIME1, bcd, S390_RT1_NBYTES);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
s390rtc_read(struct s390rtc_softc *sc, int reg, uint8_t *buf, size_t len)
|
|
|
|
{
|
2020-01-02 20:17:36 +03:00
|
|
|
int i, error;
|
2011-04-04 21:58:40 +04:00
|
|
|
|
2020-01-02 20:17:36 +03:00
|
|
|
if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) {
|
2011-04-04 21:58:40 +04:00
|
|
|
aprint_error_dev(sc->sc_dev,
|
|
|
|
"%s: failed to acquire I2C bus\n", __func__);
|
2020-01-02 20:17:36 +03:00
|
|
|
return error;
|
2011-04-04 21:58:40 +04:00
|
|
|
}
|
|
|
|
|
2020-01-02 20:17:36 +03:00
|
|
|
if ((error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
|
|
|
|
sc->sc_addr + reg, NULL, 0, buf, len, 0)) != 0) {
|
|
|
|
iic_release_bus(sc->sc_tag, 0);
|
2011-04-04 21:58:40 +04:00
|
|
|
aprint_error_dev(sc->sc_dev,
|
|
|
|
"%s: failed to read reg%d\n", __func__, reg);
|
2020-01-02 20:17:36 +03:00
|
|
|
return error;
|
2011-04-04 21:58:40 +04:00
|
|
|
}
|
|
|
|
|
2020-01-02 20:17:36 +03:00
|
|
|
iic_release_bus(sc->sc_tag, 0);
|
2011-04-04 21:58:40 +04:00
|
|
|
|
|
|
|
/* this chip returns each byte in reverse order */
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
buf[i] = bitreverse(buf[i]);
|
|
|
|
|
2020-01-02 20:17:36 +03:00
|
|
|
return 0;
|
2011-04-04 21:58:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
s390rtc_write(struct s390rtc_softc *sc, int reg, uint8_t *buf, size_t len)
|
|
|
|
{
|
2020-01-02 20:17:36 +03:00
|
|
|
int i, error;
|
2011-04-04 21:58:40 +04:00
|
|
|
|
2020-01-02 20:17:36 +03:00
|
|
|
if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) {
|
2011-04-04 21:58:40 +04:00
|
|
|
aprint_error_dev(sc->sc_dev,
|
|
|
|
"%s: failed to acquire I2C bus\n", __func__);
|
2020-01-02 20:17:36 +03:00
|
|
|
return error;
|
2011-04-04 21:58:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* this chip expects each byte in reverse order */
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
buf[i] = bitreverse(buf[i]);
|
|
|
|
|
2020-01-02 20:17:36 +03:00
|
|
|
if ((error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP,
|
|
|
|
sc->sc_addr + reg, NULL, 0, buf, len, 0)) != 0) {
|
|
|
|
iic_release_bus(sc->sc_tag, 0);
|
2011-04-04 21:58:40 +04:00
|
|
|
aprint_error_dev(sc->sc_dev,
|
|
|
|
"%s: failed to write reg%d\n", __func__, reg);
|
2020-01-02 20:17:36 +03:00
|
|
|
return error;
|
2011-04-04 21:58:40 +04:00
|
|
|
}
|
|
|
|
|
2020-01-02 20:17:36 +03:00
|
|
|
iic_release_bus(sc->sc_tag, 0);
|
|
|
|
return 0;
|
2011-04-04 21:58:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t
|
|
|
|
bitreverse(uint8_t x)
|
|
|
|
{
|
|
|
|
static unsigned char nibbletab[16] = {
|
|
|
|
0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
|
|
|
|
};
|
|
|
|
|
|
|
|
return (nibbletab[x & 15] << 4) | nibbletab[x >> 4];
|
|
|
|
}
|