#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: mkclock.c,v 1.14 2025/09/07 21:45:15 thorpej Exp $");
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/proc.h>
#include <sys/resourcevar.h>
#include <sys/systm.h>
#include <uvm/uvm_extern.h>
#include <sys/bus.h>
#include <machine/autoconf.h>
#include <machine/eeprom.h>
#include <machine/cpu.h>
#include <dev/clock_subr.h>
#include <dev/ic/mk48txxreg.h>
#include <dev/ic/mk48txxvar.h>
#include <dev/sbus/sbusvar.h>
#include <dev/ebus/ebusreg.h>
#include <dev/ebus/ebusvar.h>
#include <sparc64/dev/fhcvar.h>
static int mkclock_sbus_match(device_t, cfdata_t, void *);
static void mkclock_sbus_attach(device_t, device_t, void *);
static int mkclock_ebus_match(device_t, cfdata_t, void *);
static void mkclock_ebus_attach(device_t, device_t, void *);
static int mkclock_fhc_match(device_t, cfdata_t, void *);
static void mkclock_fhc_attach(device_t, device_t, void *);
static void mkclock_attach(struct mk48txx_softc *, int);
static int mkclock_wenable(struct todr_chip_handle *, int);
CFATTACH_DECL_NEW(mkclock_sbus, sizeof(struct mk48txx_softc),
mkclock_sbus_match, mkclock_sbus_attach, NULL, NULL);
CFATTACH_DECL_NEW(mkclock_ebus, sizeof(struct mk48txx_softc),
mkclock_ebus_match, mkclock_ebus_attach, NULL, NULL);
CFATTACH_DECL_NEW(mkclock_fhc, sizeof(struct mk48txx_softc),
mkclock_fhc_match, mkclock_fhc_attach, NULL, NULL);
static int
mkclock_sbus_match(device_t parent, cfdata_t cf, void *aux)
{
struct sbus_attach_args *sa = aux;
return (strcmp("eeprom", sa->sa_name) == 0);
}
static int
mkclock_ebus_match(device_t parent, cfdata_t cf, void *aux)
{
struct ebus_attach_args *ea = aux;
return (strcmp("eeprom", ea->ea_name) == 0);
}
static int
mkclock_fhc_match(device_t parent, cfdata_t cf, void *aux)
{
struct fhc_attach_args *fa = aux;
return (strcmp("eeprom", fa->fa_name) == 0);
}
static void
mkclock_sbus_attach(device_t parent, device_t self, void *aux)
{
struct mk48txx_softc *sc = device_private(self);
struct sbus_attach_args *sa = aux;
int sz;
sc->sc_dev = self;
sc->sc_bst = sa->sa_bustag;
sz = 8192;
if (sbus_bus_map(sc->sc_bst,
sa->sa_slot,
trunc_page(sa->sa_offset),
sz,
BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_READONLY,
&sc->sc_bsh) != 0) {
aprint_error(": can't map register\n");
return;
}
mkclock_attach(sc, sa->sa_node);
}
static void
mkclock_ebus_attach(device_t parent, device_t self, void *aux)
{
struct mk48txx_softc *sc = device_private(self);
struct ebus_attach_args *ea = aux;
int sz;
sc->sc_dev = self;
sc->sc_bst = ea->ea_bustag;
sz = ea->ea_reg[0].size;
if (ea->ea_nvaddr)
sparc_promaddr_to_handle(sc->sc_bst, ea->ea_vaddr[0],
&sc->sc_bsh);
else if (bus_space_map(sc->sc_bst,
EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
sz,
BUS_SPACE_MAP_LINEAR,
&sc->sc_bsh) != 0) {
aprint_error(": can't map register\n");
return;
}
mkclock_attach(sc, ea->ea_node);
}
static void
mkclock_fhc_attach(device_t parent, device_t self, void *aux)
{
struct mk48txx_softc *sc = device_private(self);
struct fhc_attach_args *fa = aux;
sc->sc_dev = self;
sc->sc_bst = fa->fa_bustag;
if (fhc_bus_map(sc->sc_bst,
fa->fa_reg[0].fbr_slot,
(fa->fa_reg[0].fbr_offset & ~NBPG),
fa->fa_reg[0].fbr_size,
BUS_SPACE_MAP_LINEAR,
&sc->sc_bsh) != 0) {
aprint_error(": can't map register\n");
return;
}
mkclock_attach(sc, fa->fa_node);
}
static void
mkclock_attach(struct mk48txx_softc *sc, int node)
{
sc->sc_model = prom_getpropstring(node, "model");
#ifdef DIAGNOSTIC
if (sc->sc_model == NULL)
panic("clockattach: no model property");
#endif
sc->sc_year0 = 1968;
sc->sc_handle.todr_setwen = mkclock_wenable;
mk48txx_attach(sc);
aprint_normal("\n");
}
static int
mkclock_wenable(struct todr_chip_handle *handle, int onoff)
{
struct mk48txx_softc *sc = device_private(handle->todr_dev);
vm_prot_t prot;
vaddr_t va;
int s, err = 0;
static int writers;
s = splhigh();
if (onoff)
prot = writers++ == 0 ? VM_PROT_READ|VM_PROT_WRITE : 0;
else
prot = --writers == 0 ? VM_PROT_READ : 0;
splx(s);
if (prot == VM_PROT_NONE) {
return 0;
}
va = (vaddr_t)bus_space_vaddr(sc->sc_bst, sc->sc_bsh);
if (va == 0UL) {
printf("clock_wenable: WARNING -- cannot get va\n");
return EIO;
}
pmap_kprotect(va, prot);
return (err);
}