#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/lock.h>
#include <sys/module.h>
#include <sys/sx.h>
#ifdef FDT
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#endif
#include <dev/iicbus/iiconf.h>
#include <dev/iicbus/iicbus.h>
#include "clock_if.h"
#include "iicbus_if.h"
#define ISL12XX_SC_REG 0x00
#define ISL12XX_SR_REG 0x07
#define ISL12XX_SR_ARST (1u << 7)
#define ISL12XX_SR_XTOSCB (1u << 5)
#define ISL12XX_SR_WRTC (1u << 4)
#define ISL12XX_SR_EVT (1u << 3)
#define ISL12XX_SR_ALM (1u << 2)
#define ISL12XX_SR_BAT (1u << 1)
#define ISL12XX_SR_RTCF (1u << 0)
#define ISL12XX_SR_W0C_BITS (ISL12XX_SR_BAT | ISL12XX_SR_ALM | ISL12XX_SR_EVT)
#define ISL12XX_INT_REG 0x08
#define ISL12XX_INT_IM (1u << 7)
#define ISL12XX_INT_ALME (1u << 6)
#define ISL12XX_INT_LPMODE (1u << 5)
#define ISL12XX_INT_FOBATB (1u << 4)
#define ISL12XX_INT_FO_SHIFT 0
#define ISL12XX_INT_FO_MASK 0x0f
#define ISL12XX_EV_REG 0x09
#define ISL12XX_EV_EVIENB (1u << 7)
#define ISL12XX_EV_EVBATB (1u << 6)
#define ISL12XX_EV_RTCHLT (1u << 5)
#define ISL12XX_EV_EVEN (1u << 4)
#define ISL12XX_EV_EHYS_SHIFT 2
#define ISL12XX_EV_EHYS_MASK 0x03
#define ISL12XX_EV_ESMP_SHIFT 0
#define ISL12XX_EV_ESMP_MASK 0x03
#define ISL12XX_ATR_REG 0x0a
#define ISL12XX_DTR_REG 0x0b
#define ISL12XX_SCA_REG 0x0c
#define ISL12XX_USR1_REG 0x12
#define ISL12XX_USR2_REG 0x13
#define ISL12XX_SCT_REG 0x14
#define ISL12XX_24HR_FLAG (1u << 7)
#define ISL12XX_PM_FLAG (1u << 5)
#define ISL12xx_12HR_MASK 0x1f
#define ISL12xx_24HR_MASK 0x3f
struct time_regs {
uint8_t sec, min, hour, day, month, year;
};
struct isl12xx_softc {
device_t dev;
device_t busdev;
struct intr_config_hook
init_hook;
bool use_ampm;
};
#ifdef FDT
static struct ofw_compat_data compat_data[] = {
{"isil,isl1209", 1},
{"isil,isl1218", 1},
{"isil,isl1219", 1},
{"isil,isl1220", 1},
{"isil,isl1221", 1},
{NULL, 0},
};
#endif
#define WAITFLAGS (IIC_WAIT | IIC_RECURSIVE)
static inline int
isl12xx_read1(struct isl12xx_softc *sc, uint8_t reg, uint8_t *data)
{
return (iicdev_readfrom(sc->dev, reg, data, 1, WAITFLAGS));
}
static inline int
isl12xx_write1(struct isl12xx_softc *sc, uint8_t reg, uint8_t val)
{
return (iicdev_writeto(sc->dev, reg, &val, 1, WAITFLAGS));
}
static void
isl12xx_init(void *arg)
{
struct isl12xx_softc *sc = arg;
uint8_t sreg;
config_intrhook_disestablish(&sc->init_hook);
isl12xx_read1(sc, ISL12XX_SR_REG, &sreg);
if (sreg & ISL12XX_SR_RTCF) {
device_printf(sc->dev,
"RTC clock stopped; check battery\n");
}
clock_register_flags(sc->dev, 1000000, CLOCKF_SETTIME_NO_ADJ);
clock_schedule(sc->dev, 1);
}
static int
isl12xx_probe(device_t dev)
{
#ifdef FDT
if (!ofw_bus_status_okay(dev))
return (ENXIO);
if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) {
device_set_desc(dev, "Intersil ISL12xx RTC");
return (BUS_PROBE_DEFAULT);
}
#endif
return (ENXIO);
}
static int
isl12xx_attach(device_t dev)
{
struct isl12xx_softc *sc = device_get_softc(dev);
sc->dev = dev;
sc->busdev = device_get_parent(sc->dev);
sc->init_hook.ich_func = isl12xx_init;
sc->init_hook.ich_arg = sc;
if (config_intrhook_establish(&sc->init_hook) != 0)
return (ENOMEM);
return (0);
}
static int
isl12xx_detach(device_t dev)
{
clock_unregister(dev);
return (0);
}
static int
isl12xx_gettime(device_t dev, struct timespec *ts)
{
struct isl12xx_softc *sc = device_get_softc(dev);
struct bcd_clocktime bct;
struct time_regs tregs;
int err;
uint8_t hourmask, sreg;
if ((err = iicbus_request_bus(sc->busdev, sc->dev, IIC_WAIT)) == 0) {
if ((err = isl12xx_read1(sc, ISL12XX_SR_REG, &sreg)) == 0) {
err = iicdev_readfrom(sc->dev, ISL12XX_SC_REG, &tregs,
sizeof(tregs), WAITFLAGS);
}
iicbus_release_bus(sc->busdev, sc->dev);
}
if (err != 0)
return (err);
if (sreg & ISL12XX_SR_RTCF)
return (EINVAL);
if (tregs.hour & ISL12XX_24HR_FLAG) {
hourmask = ISL12xx_24HR_MASK;
} else {
sc->use_ampm = true;
hourmask = ISL12xx_12HR_MASK;
}
bct.nsec = 0;
bct.sec = tregs.sec;
bct.min = tregs.min;
bct.hour = tregs.hour & hourmask;
bct.day = tregs.day;
bct.mon = tregs.month;
bct.year = tregs.year;
bct.ispm = tregs.hour & ISL12XX_PM_FLAG;
clock_dbgprint_bcd(sc->dev, CLOCK_DBG_READ, &bct);
return (clock_bcd_to_ts(&bct, ts, sc->use_ampm));
}
static int
isl12xx_settime(device_t dev, struct timespec *ts)
{
struct isl12xx_softc *sc = device_get_softc(dev);
struct bcd_clocktime bct;
struct time_regs tregs;
int err;
uint8_t ampmflags, sreg;
ts->tv_sec -= utc_offset();
ts->tv_nsec = 0;
clock_ts_to_bcd(ts, &bct, sc->use_ampm);
clock_dbgprint_bcd(sc->dev, CLOCK_DBG_WRITE, &bct);
if (!sc->use_ampm)
ampmflags = ISL12XX_24HR_FLAG;
else
ampmflags = bct.ispm ? ISL12XX_PM_FLAG : 0;
tregs.sec = bct.sec;
tregs.min = bct.min;
tregs.hour = bct.hour | ampmflags;
tregs.day = bct.day;
tregs.month = bct.mon;
tregs.year = bct.year % 100;
if ((err = iicbus_request_bus(sc->busdev, sc->dev, IIC_WAIT)) != 0)
return (err);
if ((err = isl12xx_read1(sc, ISL12XX_SR_REG, &sreg)) == 0) {
sreg |= ISL12XX_SR_WRTC | ISL12XX_SR_W0C_BITS;
if ((err = isl12xx_write1(sc, ISL12XX_SR_REG, sreg)) == 0) {
err = iicdev_writeto(sc->dev, ISL12XX_SC_REG, &tregs,
sizeof(tregs), WAITFLAGS);
sreg &= ~ISL12XX_SR_WRTC;
isl12xx_write1(sc, ISL12XX_SR_REG, sreg);
}
}
iicbus_release_bus(sc->busdev, sc->dev);
return (err);
}
static device_method_t isl12xx_methods[] = {
DEVMETHOD(device_probe, isl12xx_probe),
DEVMETHOD(device_attach, isl12xx_attach),
DEVMETHOD(device_detach, isl12xx_detach),
DEVMETHOD(clock_gettime, isl12xx_gettime),
DEVMETHOD(clock_settime, isl12xx_settime),
DEVMETHOD_END,
};
static driver_t isl12xx_driver = {
"isl12xx",
isl12xx_methods,
sizeof(struct isl12xx_softc),
};
DRIVER_MODULE(isl12xx, iicbus, isl12xx_driver, NULL, NULL);
MODULE_VERSION(isl12xx, 1);
MODULE_DEPEND(isl12xx, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
IICBUS_FDT_PNP_INFO(compat_data);