#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: mkclock.c,v 1.21 2025/09/07 21:45:15 thorpej Exp $");
#include "opt_sparc_arch.h"
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/systm.h>
#include <uvm/uvm_extern.h>
#include <sys/bus.h>
#include <machine/autoconf.h>
#include <machine/eeprom.h>
#include <machine/promlib.h>
#include <machine/cpu.h>
#include <sparc/dev/bootbusvar.h>
#include <sparc/sparc/timervar.h>
#include <dev/clock_subr.h>
#include <dev/ic/mk48txxreg.h>
#include <dev/ic/mk48txxvar.h>
static bus_space_handle_t mk_nvram_base;
static bus_size_t mk_nvram_size;
static int mk_clk_wenable(todr_chip_handle_t, int);
static int mk_nvram_wenable(int);
static int clockmatch_mainbus(device_t, cfdata_t, void *);
static int clockmatch_obio(device_t, cfdata_t, void *);
static int clockmatch_bootbus(device_t, cfdata_t, void *);
static void clockattach_mainbus(device_t, device_t, void *);
static void clockattach_obio(device_t, device_t, void *);
static void clockattach_bootbus(device_t, device_t, void *);
static void clockattach(struct mk48txx_softc *, int);
CFATTACH_DECL_NEW(clock_mainbus, sizeof(struct mk48txx_softc),
clockmatch_mainbus, clockattach_mainbus, NULL, NULL);
CFATTACH_DECL_NEW(clock_obio, sizeof(struct mk48txx_softc),
clockmatch_obio, clockattach_obio, NULL, NULL);
CFATTACH_DECL_NEW(clock_bootbus, sizeof(struct mk48txx_softc),
clockmatch_bootbus, clockattach_bootbus, NULL, NULL);
static int
clockmatch_mainbus(device_t parent, cfdata_t cf, void *aux)
{
struct mainbus_attach_args *ma = aux;
return (strcmp("eeprom", ma->ma_name) == 0);
}
static int
clockmatch_obio(device_t parent, cfdata_t cf, void *aux)
{
union obio_attach_args *uoba = aux;
struct obio4_attach_args *oba;
if (uoba->uoba_isobio4 == 0)
return (strcmp("eeprom", uoba->uoba_sbus.sa_name) == 0);
if (!CPU_ISSUN4) {
printf("clockmatch_obio: attach args mixed up\n");
return (0);
}
if (cpuinfo.cpu_type != CPUTYP_4_300 &&
cpuinfo.cpu_type != CPUTYP_4_400)
return (0);
oba = &uoba->uoba_oba4;
return (bus_space_probe(oba->oba_bustag, oba->oba_paddr,
1,
0,
0,
NULL, NULL));
}
static int
clockmatch_bootbus(device_t parent, cfdata_t cf, void *aux)
{
struct bootbus_attach_args *baa = aux;
return (strcmp("eeprom", baa->ba_name) == 0);
}
static void
clockattach_mainbus(device_t parent, device_t self, void *aux)
{
struct mk48txx_softc *sc = device_private(self);
struct mainbus_attach_args *ma = aux;
sc->sc_dev = self;
sc->sc_bst = ma->ma_bustag;
if (bus_space_map(sc->sc_bst,
ma->ma_paddr,
ma->ma_size,
BUS_SPACE_MAP_LINEAR,
&sc->sc_bsh) != 0) {
aprint_error(": can't map register\n");
return;
}
clockattach(sc, ma->ma_node);
}
static void
clockattach_obio(device_t parent, device_t self, void *aux)
{
struct mk48txx_softc *sc = device_private(self);
union obio_attach_args *uoba = aux;
int node;
sc->sc_dev = self;
if (uoba->uoba_isobio4 == 0) {
struct sbus_attach_args *sa = &uoba->uoba_sbus;
node = sa->sa_node;
sc->sc_bst = sa->sa_bustag;
if (sbus_bus_map(sc->sc_bst,
sa->sa_slot, sa->sa_offset, sa->sa_size,
BUS_SPACE_MAP_LINEAR, &sc->sc_bsh) != 0) {
aprint_error(": can't map register\n");
return;
}
} else {
struct obio4_attach_args *oba = &uoba->uoba_oba4;
node = 0;
sc->sc_bst = oba->oba_bustag;
if (bus_space_map(sc->sc_bst,
oba->oba_paddr,
2048,
BUS_SPACE_MAP_LINEAR,
&sc->sc_bsh) != 0) {
aprint_error(": can't map register\n");
return;
}
}
clockattach(sc, node);
}
static void
clockattach_bootbus(device_t parent, device_t self, void *aux)
{
struct mk48txx_softc *sc = device_private(self);
struct bootbus_attach_args *baa = aux;
sc->sc_bst = baa->ba_bustag;
sc->sc_dev = self;
if (bus_space_map(sc->sc_bst,
BUS_ADDR(baa->ba_reg[0].oa_space,
baa->ba_reg[0].oa_base),
baa->ba_reg[0].oa_size,
BUS_SPACE_MAP_LINEAR,
&sc->sc_bsh) != 0) {
aprint_error(": can't map register\n");
return;
}
clockattach(sc, baa->ba_node);
}
static void
clockattach(struct mk48txx_softc *sc, int node)
{
if (CPU_ISSUN4)
sc->sc_model = "mk48t02";
else if (node != 0)
sc->sc_model = prom_getpropstring(node, "model");
else
panic("clockattach: node == 0");
sc->sc_year0 = 1968;
mk48txx_attach(sc);
aprint_normal("\n");
mk_nvram_base = sc->sc_bsh;
if (mk48txx_get_nvram_size(&sc->sc_handle, &mk_nvram_size) != 0)
panic("Cannot get nvram size on %s", sc->sc_model);
sc->sc_handle.todr_setwen = mk_clk_wenable;
#if defined(SUN4)
if (CPU_ISSUN4) {
if (cpuinfo.cpu_type == CPUTYP_4_300 ||
cpuinfo.cpu_type == CPUTYP_4_400) {
eeprom_va = bus_space_vaddr(sc->sc_bst, sc->sc_bsh);
eeprom_nvram_wenable = mk_nvram_wenable;
}
}
#endif
}
int
mk_clk_wenable(todr_chip_handle_t handle, int onoff)
{
return (mk_nvram_wenable(onoff));
}
int
mk_nvram_wenable(int onoff)
{
int s;
vm_prot_t prot;
vaddr_t base;
vsize_t size;
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);
size = round_page((vsize_t)mk_nvram_size);
base = trunc_page((vaddr_t)mk_nvram_base);
if (prot)
pmap_kprotect(base, size, prot);
return (0);
}