#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: clock_ebus.c,v 1.9 2025/09/07 21:45:12 thorpej Exp $");
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/systm.h>
#include <sys/timetc.h>
#include <dev/clock_subr.h>
#include <emips/ebus/ebusvar.h>
#include <emips/emips/machdep.h>
#include <machine/emipsreg.h>
struct eclock_softc {
device_t sc_dev;
struct _Tc *sc_dp;
uint32_t sc_reload;
struct timecounter sc_tc;
struct todr_chip_handle sc_todr;
};
static int eclock_ebus_match(device_t, cfdata_t, void *);
static void eclock_ebus_attach(device_t, device_t, void *);
CFATTACH_DECL_NEW(eclock_ebus, sizeof (struct eclock_softc),
eclock_ebus_match, eclock_ebus_attach, NULL, NULL);
void eclock_init(device_t);
static void __eclock_init(device_t);
static int eclock_gettime(struct todr_chip_handle *, struct timeval *);
static int eclock_settime(struct todr_chip_handle *, struct timeval *);
static int eclock_ebus_intr(void *, void *);
static u_int eclock_counter(struct timecounter *);
device_t clockdev = NULL;
void
eclock_init(device_t dev)
{
if (dev == NULL)
dev = clockdev;
if (dev == NULL)
panic("eclock_init");
__eclock_init(dev);
}
static void
__eclock_init(device_t dev)
{
struct eclock_softc *sc = device_private(dev);
struct _Tc *tc = sc->sc_dp;
uint32_t reload = 10 * 1000000;
if (hz > 1) {
uint32_t r = reload / hz;
if ((r * hz) != reload)
printf("%s: %d Hz clock will cause roundoffs"
" with 10MHz xtal (%d)\n",
device_xname(sc->sc_dev), hz, reload - (r * hz));
reload = r;
}
sc->sc_reload = reload;
tc->DownCounterHigh = 0;
tc->DownCounter = sc->sc_reload;
tc->Control = TCCT_ENABLE | TCCT_INT_ENABLE;
}
static int
eclock_gettime(struct todr_chip_handle *todr, struct timeval *tv)
{
struct eclock_softc *sc = device_private(todr->todr_dev);
struct _Tc *tc = sc->sc_dp;
uint64_t free;
int s;
s = splhigh();
free = tc->FreeRunning;
splx(s);
#if 1
{
uint64_t freeS, freeU;
freeS = free / 10000000UL;
freeU = free % 10000000UL;
tv->tv_sec = freeS;
tv->tv_usec = freeU / 10;
#if 0
printf("egt: s x%" PRIx64 " u x%lx (fs %" PRId64
" fu %" PRId64 " f %" PRId64 ")\n",
tv->tv_sec, tv->tv_usec, freeS, freeU, free);
#endif
}
#else
{
u_quad_t r;
u_quad_t d = __qdivrem(free,(u_quad_t)10000000,&r);
uint32_t su, uu;
su = (uint32_t)d;
uu = (uint32_t)r;
uu = uu / 10;
tv->tv_sec = su;
tv->tv_usec = uu;
#if 0
printf("egt: s x%" PRIx64 " u x%lx (fs %" PRId64
" fu %" PRId64 " f %" PRId64 ")\n",
tv->tv_sec, tv->tv_usec, d, r, free);
#endif
}
#endif
return 0;
}
static int
eclock_settime(struct todr_chip_handle *todr, struct timeval *tv)
{
struct eclock_softc *sc = device_private(todr->todr_dev);
struct _Tc *tc = sc->sc_dp;
uint64_t free, su;
uint32_t uu;
int s;
s = splhigh();
su = (uint64_t)tv->tv_sec;
uu = (uint32_t)tv->tv_usec;
free = su * 10 * 1000 * 1000;
free += uu * 10;
tc->FreeRunning = free;
splx(s);
#if 0
#endif
#if 0
printf("est: s x%" PRIx64 " u x%lx (%d %d), free %" PRId64 "\n",
tv->tv_sec, tv->tv_usec, su, uu, free);
#endif
return 0;
}
static int
eclock_ebus_intr(void *cookie, void *f)
{
struct eclock_softc *sc = cookie;
struct _Tc *tc = sc->sc_dp;
struct clockframe *cf = f;
volatile uint32_t x __unused;
x = tc->Control;
tc->DownCounterHigh = 0;
tc->DownCounter = sc->sc_reload;
hardclock(cf);
emips_clock_evcnt.ev_count++;
return 0;
}
static u_int
eclock_counter(struct timecounter *tc)
{
struct eclock_softc *sc = tc->tc_priv;
struct _Tc *Tc = sc->sc_dp;
return (u_int)Tc->FreeRunning;
}
static int
eclock_ebus_match(device_t parent, cfdata_t cf, void *aux)
{
struct ebus_attach_args *ia = aux;
struct _Tc *mc = (struct _Tc *)ia->ia_vaddr;
if (strcmp("eclock", ia->ia_name) != 0)
return 0;
if ((mc == NULL) ||
(mc->Tag != PMTTAG_TIMER))
return 0;
return 1;
}
static void
eclock_ebus_attach(device_t parent, device_t self, void *aux)
{
struct eclock_softc *sc = device_private(self);
struct ebus_attach_args *ia = aux;
sc->sc_dev = self;
sc->sc_dp = (struct _Tc *)ia->ia_vaddr;
sc->sc_tc.tc_get_timecount = eclock_counter;
sc->sc_tc.tc_poll_pps = 0;
sc->sc_tc.tc_counter_mask = 0xffffffff;
sc->sc_tc.tc_frequency = 10 * 1000 * 1000;
sc->sc_tc.tc_name = "eclock";
sc->sc_tc.tc_quality = 2000;
sc->sc_tc.tc_priv = sc;
sc->sc_tc.tc_next = NULL;
#if DEBUG
printf(" virt=%p ", (void *)sc->sc_dp);
#endif
printf(": eMIPS clock\n");
sc->sc_dp->Control &= ~(TCCT_INT_ENABLE|TCCT_INTERRUPT);
ebus_intr_establish(parent, (void *)ia->ia_cookie, IPL_CLOCK,
eclock_ebus_intr, sc);
#ifdef EVCNT_COUNTERS
evcnt_attach_dynamic(&clock_intr_evcnt, EVCNT_TYPE_INTR, NULL,
device_xname(self), "intr");
#endif
clockdev = self;
memset(&sc->sc_todr, 0, sizeof sc->sc_todr);
sc->sc_todr.todr_dev = self;
sc->sc_todr.todr_gettime = eclock_gettime;
sc->sc_todr.todr_settime = eclock_settime;
todr_attach(&sc->sc_todr);
tc_init(&sc->sc_tc);
}