#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ifpga_clock.c,v 1.16 2020/05/29 12:30:39 rin Exp $");
#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/atomic.h>
#include <sys/time.h>
#include <sys/timetc.h>
#include <sys/device.h>
#include <arm/cpufunc.h>
#include <machine/intr.h>
#include <evbarm/ifpga/ifpgavar.h>
#include <evbarm/ifpga/ifpgamem.h>
#include <evbarm/ifpga/ifpgareg.h>
static int statvar = 1024 / 4;
static int statmin;
static int profmin;
static int timer2min;
static int statprev;
#define TIMER_1_CLEAR (IFPGA_TIMER1_BASE + TIMERx_CLR)
#define TIMER_1_LOAD (IFPGA_TIMER1_BASE + TIMERx_LOAD)
#define TIMER_1_VALUE (IFPGA_TIMER1_BASE + TIMERx_VALUE)
#define TIMER_1_CTRL (IFPGA_TIMER1_BASE + TIMERx_CTRL)
#define TIMER_2_CLEAR (IFPGA_TIMER2_BASE + TIMERx_CLR)
#define TIMER_2_LOAD (IFPGA_TIMER2_BASE + TIMERx_LOAD)
#define TIMER_2_VALUE (IFPGA_TIMER2_BASE + TIMERx_VALUE)
#define TIMER_2_CTRL (IFPGA_TIMER2_BASE + TIMERx_CTRL)
#define COUNTS_PER_SEC (IFPGA_TIMER1_FREQ / 16)
static u_int ifpga_get_timecount(struct timecounter *);
static struct timecounter ifpga_timecounter = {
.tc_get_timecount = ifpga_get_timecount,
.tc_counter_mask = 0xffffffff,
.tc_frequency = COUNTS_PER_SEC,
.tc_name = "ifpga",
.tc_quality = 100,
};
static volatile uint32_t ifpga_base;
extern struct ifpga_softc *ifpga_sc;
extern device_t ifpga_dev;
static int clock_started = 0;
static int load_timer(int, int);
static inline u_int
getclock(void)
{
return bus_space_read_4(ifpga_sc->sc_iot, ifpga_sc->sc_tmr_ioh,
TIMER_1_VALUE);
}
static inline u_int
getstatclock(void)
{
return bus_space_read_4(ifpga_sc->sc_iot, ifpga_sc->sc_tmr_ioh,
TIMER_2_VALUE);
}
static int
clockhandler(void *fr)
{
struct clockframe *frame = (struct clockframe *)fr;
bus_space_write_4(ifpga_sc->sc_iot, ifpga_sc->sc_tmr_ioh,
TIMER_1_CLEAR, 0);
atomic_add_32(&ifpga_base, ifpga_sc->sc_clock_count);
hardclock(frame);
return 0;
}
static int
statclockhandler(void *fr)
{
struct clockframe *frame = (struct clockframe *) fr;
int newint, r, var;
var = statvar;
do {
r = random() & (var - 1);
} while (r == 0);
newint = timer2min + r;
if (newint & ~0x0000ffff)
panic("statclockhandler: statclock variance too large");
r = (statprev - getstatclock() + 1);
if (r < newint) {
newint -= r;
r = 0;
}
else
printf("statclockhandler: Statclock overrun\n");
statprev = load_timer(IFPGA_TIMER2_BASE, newint);
statclock(frame);
if (r)
statclock(frame);
return 0;
}
static int
load_timer(int base, int intvl)
{
int control;
if (intvl & ~0x0000ffff)
panic("clock: Invalid interval");
#if defined(INTEGRATOR_CP)
control = (TIMERx_CTRL_ENABLE | TIMERx_CTRL_MODE_PERIODIC |
TIMERx_CTRL_PRESCALE_DIV16 | TIMERx_CTRL_RAISE_IRQ);
#else
control = (TIMERx_CTRL_ENABLE | TIMERx_CTRL_MODE_PERIODIC |
TIMERx_CTRL_PRESCALE_DIV16);
#endif
bus_space_write_4(ifpga_sc->sc_iot, ifpga_sc->sc_tmr_ioh,
base + TIMERx_LOAD, intvl);
bus_space_write_4(ifpga_sc->sc_iot, ifpga_sc->sc_tmr_ioh,
base + TIMERx_CTRL, control);
bus_space_write_4(ifpga_sc->sc_iot, ifpga_sc->sc_tmr_ioh,
base + TIMERx_CLR, 0);
return intvl;
}
void
setstatclockrate(int new_hz)
{
if (new_hz == stathz)
timer2min = statmin;
else
timer2min = profmin;
}
void
cpu_initclocks(void)
{
int intvl;
int statint;
int profint;
int minint;
if (hz < 50 || COUNTS_PER_SEC % hz) {
printf("cannot get %d Hz clock; using 100 Hz\n", hz);
hz = 100;
tick = 1000000 / hz;
}
if (stathz == 0)
stathz = hz;
else if (stathz < 50 || COUNTS_PER_SEC % stathz) {
printf("cannot get %d Hz statclock; using 100 Hz\n", stathz);
stathz = 100;
}
if (profhz == 0)
profhz = stathz * 5;
else if (profhz < stathz || COUNTS_PER_SEC % profhz) {
printf("cannot get %d Hz profclock; using %d Hz\n", profhz,
stathz);
profhz = stathz;
}
intvl = COUNTS_PER_SEC / hz;
statint = COUNTS_PER_SEC / stathz;
profint = COUNTS_PER_SEC / profhz;
minint = statint / 2 + 100;
while (statvar > minint)
statvar >>= 1;
intvl--;
statint--;
profint--;
statmin = statint - (statvar >> 1);
profmin = profint - (statvar >> 1);
timer2min = statmin;
statprev = statint;
printf("clock: hz=%d stathz = %d profhz = %d\n", hz, stathz, profhz);
ifpga_sc->sc_clockintr = ifpga_intr_establish(IFPGA_TIMER1_IRQ,
IPL_CLOCK, clockhandler, 0);
if (ifpga_sc->sc_clockintr == NULL)
panic("%s: Cannot install timer 1 interrupt handler",
device_xname(ifpga_dev));
ifpga_sc->sc_clock_count
= load_timer(IFPGA_TIMER1_BASE, intvl);
ifpga_sc->sc_clock_ticks_per_256us =
((((ifpga_sc->sc_clock_count * hz) / 1000) * 256) / 1000);
clock_started = 1;
ifpga_sc->sc_statclockintr = ifpga_intr_establish(IFPGA_TIMER2_IRQ,
IPL_HIGH, statclockhandler, 0);
if (ifpga_sc->sc_statclockintr == NULL)
panic("%s: Cannot install timer 2 interrupt handler",
device_xname(ifpga_dev));
load_timer(IFPGA_TIMER2_BASE, statint);
tc_init(&ifpga_timecounter);
}
static u_int
ifpga_get_timecount(struct timecounter *tc)
{
u_int base, counter;
do {
base = ifpga_base;
counter = getclock();
} while (base != ifpga_base);
return base - counter;
}
int delaycount = 50;
void
delay(u_int n)
{
if (clock_started) {
u_int starttime;
u_int curtime;
u_int delta = 0;
u_int count_max = ifpga_sc->sc_clock_count;
starttime = getclock();
n *= IFPGA_TIMER1_FREQ / 1000000;
do {
n -= delta;
curtime = getclock();
delta = curtime - starttime;
if (curtime < starttime)
delta += count_max;
starttime = curtime;
} while (n > delta);
} else {
volatile u_int i;
if (n == 0) return;
while (n-- > 0) {
if (cputype == CPU_ID_SA110)
for (i = delaycount; --i;)
;
else
for (i = 8; --i;)
;
}
}
}