#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: footbridge_clock.c,v 1.28 2025/12/16 18:24:47 andvar Exp $");
#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/time.h>
#include <sys/timetc.h>
#include <sys/device.h>
#include <machine/intr.h>
#include <arm/cpufunc.h>
#include <arm/footbridge/dc21285reg.h>
#include <arm/footbridge/footbridgevar.h>
#include <arm/footbridge/footbridge.h>
extern struct footbridge_softc *clock_sc;
extern u_int dc21285_fclk;
int clockhandler(void *);
int statclockhandler(void *);
static int load_timer(int, int);
const int statvar = 1024;
int statmin;
int statcountperusec;
int statprev;
void footbridge_tc_init(void);
#if 0
static int clockmatch(device_t parent, cfdata_t cf, void *aux);
static void clockattach(device_t parent, device_t self, void *aux);
CFATTACH_DECL_NEW(footbridge_clock, sizeof(struct clock_softc),
clockmatch, clockattach, NULL, NULL);
static int
clockmatch(device_t parent, cfdata_t cf, void *aux)
{
union footbridge_attach_args *fba = aux;
if (strcmp(fba->fba_ca.ca_name, "clk") == 0)
return 1;
return 0;
}
static void
clockattach(device_t parent, device_t self, void *aux)
{
struct clock_softc *sc = device_private(self);
union footbridge_attach_args *fba = aux;
sc->sc_dev = self;
sc->sc_iot = fba->fba_ca.ca_iot;
sc->sc_ioh = fba->fba_ca.ca_ioh;
clock_sc = sc;
aprint_normal("\n");
}
#endif
int
clockhandler(void *aframe)
{
struct clockframe *frame = aframe;
bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
TIMER_1_CLEAR, 0);
hardclock(frame);
return 0;
}
int
statclockhandler(void *aframe)
{
struct clockframe *frame = aframe;
int newint, r;
int currentclock ;
bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
TIMER_2_CLEAR, 0);
do {
r = random() & (statvar-1);
} while (r == 0);
newint = statmin + (r * statcountperusec);
currentclock = bus_space_read_4(clock_sc->sc_iot, clock_sc->sc_ioh,
TIMER_2_VALUE);
r = ((statprev - currentclock) + statcountperusec);
if (r < newint) {
newint -= r;
r = 0;
}
else
printf("statclockhandler: Statclock overrun\n");
bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
TIMER_2_LOAD, newint);
statprev = newint;
statclock(frame);
if (r)
statclock(frame);
return 0;
}
static int
load_timer(int base, int herz)
{
unsigned int timer_count;
int control;
timer_count = dc21285_fclk / herz;
if (timer_count > TIMER_MAX_VAL * 16) {
control = TIMER_FCLK_256;
timer_count >>= 8;
} else if (timer_count > TIMER_MAX_VAL) {
control = TIMER_FCLK_16;
timer_count >>= 4;
} else
control = TIMER_FCLK;
control |= (TIMER_ENABLE | TIMER_MODE_PERIODIC);
bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
base + TIMER_LOAD, timer_count);
bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
base + TIMER_CONTROL, control);
bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
base + TIMER_CLEAR, 0);
return timer_count;
}
void
setstatclockrate(int herz)
{
int statint;
int countpersecond;
int statvarticks;
statint = statprev = clock_sc->sc_statclock_count =
load_timer(TIMER_2_BASE, herz);
countpersecond = statint * herz;
statcountperusec = countpersecond / 1000000;
statvarticks = statcountperusec * statvar;
statmin = statint - (statvarticks / 2);
}
void
cpu_initclocks(void)
{
if (stathz == 0)
stathz = hz;
if (profhz == 0)
profhz = stathz * 5;
aprint_debug("clock: hz=%d stathz = %d profhz = %d\n", hz, stathz, profhz);
clock_sc->sc_clock_count = load_timer(TIMER_1_BASE, hz);
clock_sc->sc_clock_ticks_per_256us =
((((clock_sc->sc_clock_count * hz) / 1000) * 256) / 1000);
clock_sc->sc_clockintr = footbridge_intr_claim(IRQ_TIMER_1, IPL_CLOCK,
"tmr1 hard clk", clockhandler, 0);
if (clock_sc->sc_clockintr == NULL)
panic("%s: Cannot install timer 1 interrupt handler",
device_xname(clock_sc->sc_dev));
if (stathz) {
setstatclockrate(stathz);
clock_sc->sc_statclockintr = footbridge_intr_claim(IRQ_TIMER_2, IPL_HIGH,
"tmr2 stat clk", statclockhandler, 0);
if (clock_sc->sc_statclockintr == NULL)
panic("%s: Cannot install timer 2 interrupt handler",
device_xname(clock_sc->sc_dev));
}
footbridge_tc_init();
}
static uint32_t
fclk_get_count(struct timecounter *tc)
{
return (TIMER_MAX_VAL -
bus_space_read_4(clock_sc->sc_iot, clock_sc->sc_ioh,
TIMER_3_VALUE));
}
void
footbridge_tc_init(void)
{
static struct timecounter fb_tc = {
.tc_get_timecount = fclk_get_count,
.tc_counter_mask = TIMER_MAX_VAL,
.tc_name = "dc21285_fclk",
.tc_quality = 100
};
fb_tc.tc_frequency = dc21285_fclk;
tc_init(&fb_tc);
}
static int delay_count_per_usec = 0;
void
calibrate_delay(void)
{
bus_space_write_4(clock_sc->sc_iot, clock_sc->sc_ioh,
TIMER_3_BASE + TIMER_CONTROL, TIMER_ENABLE);
delay_count_per_usec = dc21285_fclk / 1000000;
if (dc21285_fclk % 1000000)
delay_count_per_usec += 1;
}
void
delay(unsigned n)
{
uint32_t cur, last, delta, usecs;
if (n == 0)
return;
if (!delay_count_per_usec)
{
int delaycount = 40;
volatile int i;
while (n-- > 0) {
for (i = delaycount; --i;);
}
return;
}
last = bus_space_read_4(clock_sc->sc_iot, clock_sc->sc_ioh,
TIMER_3_VALUE);
delta = usecs = 0;
while (n > usecs) {
cur = bus_space_read_4(clock_sc->sc_iot, clock_sc->sc_ioh,
TIMER_3_VALUE);
if (last < cur)
delta += ((TIMER_MAX_VAL - cur) + last);
else
delta += (last - cur);
last = cur;
while (delta >= delay_count_per_usec) {
delta -= delay_count_per_usec;
usecs++;
}
}
}