#include "opt_todr.h"
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_todr.c,v 1.52 2026/01/04 02:09:03 riastradh Exp $");
#include <sys/param.h>
#include <sys/device.h>
#include <sys/device_calls.h>
#include <sys/intr.h>
#include <sys/kernel.h>
#include <sys/mutex.h>
#include <sys/rndsource.h>
#include <sys/sdt.h>
#include <sys/systm.h>
#include <sys/timetc.h>
#include <dev/clock_subr.h>
static int todr_gettime(todr_chip_handle_t, struct timeval *);
static int todr_settime(todr_chip_handle_t, struct timeval *);
static kmutex_t todr_mutex;
static todr_chip_handle_t todr_handle;
static bool todr_initialized;
#define PREPOSTEROUS_YEARS (2021 - POSIX_BASE_YEAR)
void
todr_init(void)
{
mutex_init(&todr_mutex, MUTEX_DEFAULT, IPL_NONE);
todr_initialized = true;
}
void
todr_lock(void)
{
mutex_enter(&todr_mutex);
}
void
todr_unlock(void)
{
mutex_exit(&todr_mutex);
}
bool
todr_lock_owned(void)
{
return mutex_owned(&todr_mutex) ? true : false;
}
static bool
todr_should_skip(todr_chip_handle_t todr)
{
device_t dev = todr->todr_dev;
struct device_is_system_todr_args args = { };
if (dev == NULL) {
return false;
}
if (device_call(dev, DEVICE_IS_SYSTEM_TODR(&args)) != 0) {
return false;
}
if (args.result) {
return false;
}
aprint_normal_dev(dev, "disabled (not system TODR)\n");
return true;
}
void
todr_attach(todr_chip_handle_t todr)
{
KASSERT(todr_initialized);
if (todr_should_skip(todr)) {
return;
}
todr_lock();
if (todr_handle) {
todr_unlock();
printf("todr_attach: TOD already configured\n");
return;
}
todr_handle = todr;
todr_unlock();
}
static bool timeset = false;
void
todr_set_systime(time_t base)
{
bool badbase = false;
bool waszero = (base == 0);
bool goodtime = false;
bool badrtc = false;
struct timespec ts;
struct timeval tv;
KASSERT(todr_lock_owned());
rnd_add_data(NULL, &base, sizeof(base), 0);
if (base < 5 * SECS_PER_COMMON_YEAR) {
struct clock_ymdhms basedate;
if (base != 0)
printf("WARNING: preposterous time in file system\n");
basedate.dt_year = 2010;
basedate.dt_mon = 1;
basedate.dt_day = 1;
basedate.dt_hour = 12;
basedate.dt_min = 0;
basedate.dt_sec = 0;
base = clock_ymdhms_to_secs(&basedate);
badbase = true;
}
if (todr_handle)
todr_handle->todr_base_time = base;
memset(&tv, 0, sizeof(tv));
if ((todr_handle == NULL) ||
(todr_gettime(todr_handle, &tv) != 0) ||
(tv.tv_sec < (PREPOSTEROUS_YEARS * SECS_PER_COMMON_YEAR))) {
if (todr_handle != NULL)
printf("WARNING: preposterous TOD clock time\n");
else
printf("WARNING: no TOD clock present\n");
badrtc = true;
} else {
time_t deltat = tv.tv_sec - base;
if (deltat < 0)
deltat = -deltat;
if (!badbase && deltat >= 2 * SECS_PER_DAY) {
if (tv.tv_sec < base) {
printf("WARNING: clock lost %" PRId64 " days\n",
deltat / SECS_PER_DAY);
badrtc = true;
} else {
aprint_verbose("WARNING: clock gained %" PRId64
" days\n", deltat / SECS_PER_DAY);
goodtime = true;
}
} else {
goodtime = true;
}
rnd_add_data(NULL, &tv, sizeof(tv), 0);
}
if (badrtc) {
if (badbase) {
printf("WARNING: using default initial time\n");
} else {
printf("WARNING: using filesystem time\n");
}
tv.tv_sec = base;
tv.tv_usec = 0;
}
timeset = true;
ts.tv_sec = tv.tv_sec;
ts.tv_nsec = tv.tv_usec * 1000;
tc_setclock(&ts);
if (waszero || goodtime)
return;
printf("WARNING: CHECK AND RESET THE DATE!\n");
}
void
todr_save_systime(void)
{
struct timeval tv;
KASSERT(todr_lock_owned());
if (!timeset)
return;
getmicrotime(&tv);
if (tv.tv_sec == 0)
return;
if (todr_handle)
if (todr_settime(todr_handle, &tv) != 0)
printf("Cannot set TOD clock time\n");
}
void
inittodr(time_t base)
{
todr_lock();
todr_set_systime(base);
todr_unlock();
}
void
resettodr(void)
{
if (shutting_down) {
if (mutex_tryenter(&todr_mutex) == 0) {
printf("WARNING: Cannot set TOD clock time (busy)\n");
return;
}
} else {
todr_lock();
}
todr_save_systime();
todr_unlock();
}
#ifdef TODR_DEBUG
static void
todr_debug(const char *prefix, int rv, struct clock_ymdhms *dt,
struct timeval *tvp)
{
struct timeval tv_val;
struct clock_ymdhms dt_val;
if (dt == NULL) {
clock_secs_to_ymdhms(tvp->tv_sec, &dt_val);
dt = &dt_val;
}
if (tvp == NULL) {
tvp = &tv_val;
tvp->tv_sec = clock_ymdhms_to_secs(dt);
tvp->tv_usec = 0;
}
printf("%s: rv = %d\n", prefix, rv);
printf("%s: rtc_offset = %d\n", prefix, rtc_offset);
printf("%s: %4u/%02u/%02u %02u:%02u:%02u, (wday %d) (epoch %u.%06u)\n",
prefix,
(unsigned)dt->dt_year, dt->dt_mon, dt->dt_day,
dt->dt_hour, dt->dt_min, dt->dt_sec,
dt->dt_wday, (unsigned)tvp->tv_sec, (unsigned)tvp->tv_usec);
}
#else
#define todr_debug(prefix, rv, dt, tvp)
#endif
static int
todr_wenable(todr_chip_handle_t todr, int onoff)
{
if (todr->todr_setwen != NULL)
return todr->todr_setwen(todr, onoff);
return 0;
}
#define ENABLE_TODR_WRITES() \
do { \
if ((rv = todr_wenable(tch, 1)) != 0) { \
printf("%s: cannot enable TODR writes\n", __func__); \
return rv; \
} \
} while (0)
#define DISABLE_TODR_WRITES() \
do { \
if (todr_wenable(tch, 0) != 0) \
printf("%s: WARNING: cannot disable TODR writes\n", \
__func__); \
} while (0)
static int
todr_gettime(todr_chip_handle_t tch, struct timeval *tvp)
{
int rv;
if (tch->todr_gettime) {
ENABLE_TODR_WRITES();
rv = tch->todr_gettime(tch, tvp);
DISABLE_TODR_WRITES();
if (rv == 0)
tvp->tv_sec += rtc_offset * 60;
todr_debug("TODR-GET-SECS", rv, NULL, tvp);
return rv;
} else if (tch->todr_gettime_ymdhms) {
struct clock_ymdhms dt = { 0 };
ENABLE_TODR_WRITES();
rv = tch->todr_gettime_ymdhms(tch, &dt);
DISABLE_TODR_WRITES();
todr_debug("TODR-GET-YMDHMS", rv, &dt, NULL);
if (rv)
return rv;
if (dt.dt_mon < 1 || dt.dt_mon > 12 ||
dt.dt_day < 1 || dt.dt_day > 31 ||
dt.dt_hour > 23 || dt.dt_min > 59 || dt.dt_sec > 60) {
return SET_ERROR(EINVAL);
}
tvp->tv_sec = clock_ymdhms_to_secs(&dt) + rtc_offset * 60;
tvp->tv_usec = 0;
return tvp->tv_sec < 0 ? SET_ERROR(EINVAL) : 0;
}
return SET_ERROR(ENXIO);
}
static int
todr_settime(todr_chip_handle_t tch, struct timeval *tvp)
{
int rv;
if (tch->todr_settime) {
struct timeval copy = *tvp;
copy.tv_sec -= rtc_offset * 60;
ENABLE_TODR_WRITES();
rv = tch->todr_settime(tch, ©);
DISABLE_TODR_WRITES();
todr_debug("TODR-SET-SECS", rv, NULL, tvp);
return rv;
} else if (tch->todr_settime_ymdhms) {
struct clock_ymdhms dt;
time_t sec = tvp->tv_sec - rtc_offset * 60;
if (tvp->tv_usec >= 500000)
sec++;
clock_secs_to_ymdhms(sec, &dt);
ENABLE_TODR_WRITES();
rv = tch->todr_settime_ymdhms(tch, &dt);
DISABLE_TODR_WRITES();
todr_debug("TODR-SET-YMDHMS", rv, &dt, NULL);
return rv;
}
return SET_ERROR(ENXIO);
}