#include <sys/cdefs.h>
#include "opt_platform.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/clock.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <dev/iicbus/iicbus.h>
#include <dev/iicbus/iiconf.h>
#ifdef FDT
#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#endif
#include "clock_if.h"
#include "iicbus_if.h"
#define S390_DEVNAME "s35390a_rtc"
#define S390_DEVCODE 0x6
#define S390_ADDR (S390_DEVCODE << 4)
#define S390_STATUS1 (0 << 1)
#define S390_STATUS2 (1 << 1)
#define S390_REALTIME1 (2 << 1)
#define S390_REALTIME2 (3 << 1)
#define S390_INT1_1 (4 << 1)
#define S390_INT1_2 (5 << 1)
#define S390_CLOCKADJ (6 << 1)
#define S390_FREE (7 << 1)
#define S390_ST1_POC (1 << 7)
#define S390_ST1_BLD (1 << 6)
#define S390_ST1_24H (1 << 1)
#define S390_ST1_RESET (1 << 0)
#define S390_ST2_TEST (1 << 7)
#define S390_RT1_NBYTES 7
#define S390_RT1_YEAR 0
#define S390_RT1_MONTH 1
#define S390_RT1_DAY 2
#define S390_RT1_WDAY 3
#define S390_RT1_HOUR 4
#define S390_RT1_MINUTE 5
#define S390_RT1_SECOND 6
struct s390rtc_softc {
device_t sc_dev;
uint16_t sc_addr;
};
static const uint8_t nibbletab[] = {
0x0,
0x8,
0x4,
0xc,
0x2,
0xa,
0x6,
0xe,
0x1,
0x9,
0x5,
0xd,
0x3,
0xb,
0x7,
0xf, };
static uint8_t
bitreverse(uint8_t x)
{
return (nibbletab[x & 0xf] << 4) | nibbletab[x >> 4];
}
static int
s390rtc_read(device_t dev, uint8_t reg, uint8_t *buf, size_t len)
{
struct s390rtc_softc *sc = device_get_softc(dev);
struct iic_msg msg[] = {
{
.slave = sc->sc_addr | reg,
.flags = IIC_M_RD,
.len = len,
.buf = buf,
},
};
int i;
int error;
error = iicbus_transfer_excl(dev, msg, 1, IIC_WAIT);
if (error)
return (error);
for (i = 0; i < len; ++i)
buf[i] = bitreverse(buf[i]);
return (0);
}
static int
s390rtc_write(device_t dev, uint8_t reg, uint8_t *buf, size_t len)
{
struct s390rtc_softc *sc = device_get_softc(dev);
struct iic_msg msg[] = {
{
.slave = sc->sc_addr | reg,
.flags = IIC_M_WR,
.len = len,
.buf = buf,
},
};
int i;
for (i = 0; i < len; ++i)
buf[i] = bitreverse(buf[i]);
return (iicbus_transfer_excl(dev, msg, 1, IIC_WAIT));
}
static int
s390rtc_probe(device_t dev)
{
#ifdef FDT
if (!ofw_bus_status_okay(dev))
return (ENXIO);
if (!ofw_bus_is_compatible(dev, "sii,s35390a"))
return (ENXIO);
#else
if (iicbus_get_addr(dev) != S390_ADDR) {
if (bootverbose)
device_printf(dev, "slave address mismatch. "
"(%02x != %02x)\n", iicbus_get_addr(dev),
S390_ADDR);
return (ENXIO);
}
#endif
device_set_desc(dev, "Seiko Instruments S-35390A RTC");
return (BUS_PROBE_DEFAULT);
}
static void
s390rtc_start(void *arg)
{
device_t dev;
uint8_t reg;
int error;
dev = arg;
error = s390rtc_read(dev, S390_STATUS1, ®, 1);
if (error) {
device_printf(dev, "%s: cannot read status1 register\n",
__func__);
return;
}
if (reg & (S390_ST1_POC | S390_ST1_BLD)) {
reg |= S390_ST1_24H | S390_ST1_RESET;
error = s390rtc_write(dev, S390_STATUS1, ®, 1);
if (error) {
device_printf(dev,
"%s: cannot initialize\n", __func__);
return;
}
}
error = s390rtc_read(dev, S390_STATUS2, ®, 1);
if (error) {
device_printf(dev, "%s: cannot read status2 register\n",
__func__);
return;
}
if (reg & S390_ST2_TEST) {
reg &= ~S390_ST2_TEST;
error = s390rtc_write(dev, S390_STATUS2, ®, 1);
if (error) {
device_printf(dev,
"%s: cannot disable the test mode\n", __func__);
return;
}
}
clock_register(dev, 1000000);
}
static int
s390rtc_attach(device_t dev)
{
struct s390rtc_softc *sc;
sc = device_get_softc(dev);
sc->sc_dev = dev;
sc->sc_addr = iicbus_get_addr(dev);
config_intrhook_oneshot(s390rtc_start, dev);
return (0);
}
static int
s390rtc_detach(device_t dev)
{
clock_unregister(dev);
return (0);
}
static int
s390rtc_gettime(device_t dev, struct timespec *ts)
{
uint8_t bcd[S390_RT1_NBYTES];
struct bcd_clocktime bct;
int error;
error = s390rtc_read(dev, S390_REALTIME1, bcd, S390_RT1_NBYTES);
if (error) {
device_printf(dev, "%s: cannot read realtime1 register\n",
__func__);
return (error);
}
bct.nsec = 0;
bct.sec = bcd[S390_RT1_SECOND];
bct.min = bcd[S390_RT1_MINUTE];
bct.hour = bcd[S390_RT1_HOUR] & 0x3f;
bct.day = bcd[S390_RT1_DAY];
bct.dow = bcd[S390_RT1_WDAY] & 0x07;
bct.mon = bcd[S390_RT1_MONTH];
bct.year = bcd[S390_RT1_YEAR];
clock_dbgprint_bcd(dev, CLOCK_DBG_READ, &bct);
return (clock_bcd_to_ts(&bct, ts, false));
}
static int
s390rtc_settime(device_t dev, struct timespec *ts)
{
uint8_t bcd[S390_RT1_NBYTES];
struct bcd_clocktime bct;
clock_ts_to_bcd(ts, &bct, false);
clock_dbgprint_bcd(dev, CLOCK_DBG_WRITE, &bct);
bcd[S390_RT1_SECOND] = bct.sec;
bcd[S390_RT1_MINUTE] = bct.min;
bcd[S390_RT1_HOUR] = bct.hour;
bcd[S390_RT1_DAY] = bct.day;
bcd[S390_RT1_WDAY] = bct.dow;
bcd[S390_RT1_MONTH] = bct.mon;
bcd[S390_RT1_YEAR] = bct.year & 0xff;
return (s390rtc_write(dev, S390_REALTIME1, bcd, S390_RT1_NBYTES));
}
static device_method_t s390rtc_methods[] = {
DEVMETHOD(device_probe, s390rtc_probe),
DEVMETHOD(device_attach, s390rtc_attach),
DEVMETHOD(device_detach, s390rtc_detach),
DEVMETHOD(clock_gettime, s390rtc_gettime),
DEVMETHOD(clock_settime, s390rtc_settime),
DEVMETHOD_END
};
static driver_t s390rtc_driver = {
S390_DEVNAME,
s390rtc_methods,
sizeof(struct s390rtc_softc),
};
DRIVER_MODULE(s35390a, iicbus, s390rtc_driver, NULL, NULL);
MODULE_VERSION(s35390a, 1);
MODULE_DEPEND(s35390a, iicbus, 1, 1, 1);