#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: loongson_clock.c,v 1.3 2020/10/25 16:39:00 nia Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/cpu.h>
#include <sys/timetc.h>
#include <sys/sysctl.h>
#include <mips/mips3_clock.h>
#include <mips/locore.h>
#include <mips/bonito/bonitoreg.h>
#include <mips/bonito/bonitovar.h>
#ifdef LOONGSON_CLOCK_DEBUG
#define DPRINTF aprint_error
#else
#define DPRINTF while (0) printf
#endif
static uint32_t sc_last;
static uint32_t sc_scale[8];
static uint32_t sc_count;
static int sc_step = 7;
static int sc_step_wanted = 7;
static void *sc_shutdown_cookie;
static int scale_m[] = {1, 1, 3, 1, 5, 3, 7, 1};
static int scale_d[] = {0, 4, 8, 2, 8, 4, 8, 1};
static int cycles[8];
#define scale(x, f) (x * scale_d[f] / scale_m[f])
#define rscale(x, f) (x * scale_m[f] / scale_d[f])
static void loongson_set_speed(int);
static int loongson_cpuspeed_temp(SYSCTLFN_ARGS);
static int loongson_cpuspeed_cur(SYSCTLFN_ARGS);
static int loongson_cpuspeed_available(SYSCTLFN_ARGS);
static void loongson_clock_shutdown(void *);
static u_int get_loongson_timecount(struct timecounter *);
void loongson_delay(int);
void loongson_setstatclockrate(int);
void loongson_initclocks(void);
static struct timecounter loongson_timecounter = {
.tc_get_timecount = get_loongson_timecount,
.tc_counter_mask = 0xffffffff,
.tc_name = "loongson",
.tc_quality = 100,
};
void
loongson_initclocks(void)
{
const struct sysctlnode *sysctl_node, *me, *freq;
int clk;
sc_shutdown_cookie = shutdownhook_establish(loongson_clock_shutdown, NULL);
for (clk = 1; clk < 8; clk++) {
sc_scale[clk] = rscale(curcpu()->ci_cpu_freq / 1000000, clk);
cycles[clk] =
(rscale(curcpu()->ci_cpu_freq, clk) + hz / 2) / (2 * hz);
}
#ifdef LOONGSON_CLOCK_DEBUG
for (clk = 1; clk < 8; clk++) {
aprint_normal("frequencies: %d/8: %d\n", clk + 1,
sc_scale[clk]);
}
#endif
if (sysctl_createv(NULL, 0, NULL,
&me,
CTLFLAG_READWRITE, CTLTYPE_NODE, "cpu", NULL, NULL,
0, NULL, 0, CTL_MACHDEP, CTL_CREATE, CTL_EOL) != 0)
aprint_error("couldn't create 'cpu' node\n");
if (sysctl_createv(NULL, 0, NULL,
&freq,
CTLFLAG_READWRITE, CTLTYPE_NODE, "frequency", NULL, NULL, 0, NULL,
0, CTL_MACHDEP, me->sysctl_num, CTL_CREATE, CTL_EOL) != 0)
aprint_error("couldn't create 'frequency' node\n");
if (sysctl_createv(NULL, 0, NULL,
&sysctl_node,
CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
CTLTYPE_INT, "target", "CPU speed", loongson_cpuspeed_temp,
0, NULL, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
CTL_CREATE, CTL_EOL) == 0) {
} else
aprint_error("couldn't create 'target' node\n");
if (sysctl_createv(NULL, 0, NULL,
&sysctl_node,
CTLFLAG_READWRITE,
CTLTYPE_INT, "current", NULL, loongson_cpuspeed_cur,
1, NULL, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
CTL_CREATE, CTL_EOL) == 0) {
} else
aprint_error("couldn't create 'current' node\n");
if (sysctl_createv(NULL, 0, NULL,
&sysctl_node,
CTLFLAG_READWRITE,
CTLTYPE_STRING, "available", NULL, loongson_cpuspeed_available,
2, NULL, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
CTL_CREATE, CTL_EOL) == 0) {
} else
aprint_error("couldn't create 'available' node\n");
sc_count = 0;
loongson_timecounter.tc_frequency = curcpu()->ci_cpu_freq / 2;
curcpu()->ci_cctr_freq = loongson_timecounter.tc_frequency;
sc_last = mips3_cp0_count_read();
mips3_cp0_compare_write(sc_last + curcpu()->ci_cycles_per_hz);
tc_init(&loongson_timecounter);
spl0();
printf("boom\n");
}
static void
loongson_clock_shutdown(void *cookie)
{
sc_step_wanted = 7;
REGVAL(LS2F_CHIPCFG0) =
(REGVAL(LS2F_CHIPCFG0) & ~LS2FCFG_FREQSCALE_MASK) | 7;
}
void
loongson_set_speed(int speed)
{
if ((speed < 1) || (speed > 7))
return;
sc_step_wanted = speed;
DPRINTF("%s: %d\n", __func__, speed);
}
void
mips3_clockintr(struct clockframe *cf)
{
uint32_t now, diff, next, new_cnt;
if (sc_step_wanted != sc_step) {
REGVAL(LS2F_CHIPCFG0) =
(REGVAL(LS2F_CHIPCFG0) & ~LS2FCFG_FREQSCALE_MASK) |
sc_step_wanted;
}
now = mips3_cp0_count_read();
diff = now - sc_last;
sc_count += scale(diff, sc_step);
sc_last = now;
if (sc_step_wanted != sc_step) {
sc_step = sc_step_wanted;
curcpu()->ci_cycles_per_hz = cycles[sc_step];
}
next = now + curcpu()->ci_cycles_per_hz;
curcpu()->ci_ev_count_compare.ev_count++;
mips3_cp0_compare_write(next);
new_cnt = mips3_cp0_count_read();
if ((next - new_cnt) & 0x80000000) {
next = new_cnt + curcpu()->ci_cycles_per_hz;
mips3_cp0_compare_write(next);
curcpu()->ci_ev_count_compare_missed.ev_count++;
}
hardclock(cf);
}
static u_int
get_loongson_timecount(struct timecounter *tc)
{
uint32_t now, diff;
now = mips3_cp0_count_read();
diff = now - sc_last;
return sc_count + scale(diff, sc_step);
}
static int
loongson_cpuspeed_temp(SYSCTLFN_ARGS)
{
struct sysctlnode node = *rnode;
int mhz, i;
mhz = sc_scale[sc_step_wanted];
node.sysctl_data = &mhz;
if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
int new_reg;
new_reg = *(int *)node.sysctl_data;
i = 1;
while ((i < 8) && (sc_scale[i] != new_reg))
i++;
if (i > 7)
return EINVAL;
loongson_set_speed(i);
return 0;
}
return EINVAL;
}
static int
loongson_cpuspeed_cur(SYSCTLFN_ARGS)
{
struct sysctlnode node = *rnode;
int mhz;
mhz = sc_scale[sc_step];
node.sysctl_data = &mhz;
return sysctl_lookup(SYSCTLFN_CALL(&node));
}
static int
loongson_cpuspeed_available(SYSCTLFN_ARGS)
{
struct sysctlnode node = *rnode;
char buf[128];
snprintf(buf, 128, "%d %d %d %d %d %d %d", sc_scale[1],
sc_scale[2], sc_scale[3], sc_scale[4],
sc_scale[5], sc_scale[6], sc_scale[7]);
node.sysctl_data = buf;
return(sysctl_lookup(SYSCTLFN_CALL(&node)));
}
void
loongson_delay(int n)
{
u_long divisor_delay;
uint32_t cur, last, delta, usecs;
last = mips3_cp0_count_read();
delta = usecs = 0;
divisor_delay = rscale(curcpu()->ci_divisor_delay, sc_step);
if (divisor_delay == 0) {
#define FAST_FREQ (300 * 1000 * 1000)
divisor_delay = FAST_FREQ / (1000 * 1000);
}
while (n > usecs) {
cur = mips3_cp0_count_read();
delta += (cur - last);
last = cur;
while (delta >= divisor_delay) {
usecs++;
delta -= divisor_delay;
}
}
}
SYSCTL_SETUP(sysctl_ams_setup, "sysctl obio subtree setup")
{
sysctl_createv(NULL, 0, NULL, NULL,
CTLFLAG_PERMANENT,
CTLTYPE_NODE, "machdep", NULL,
NULL, 0, NULL, 0,
CTL_MACHDEP, CTL_EOL);
}
void
loongson_setstatclockrate(int newhz)
{
}
__weak_alias(setstatclockrate, loongson_setstatclockrate);
__weak_alias(cpu_initclocks, loongson_initclocks);
__weak_alias(delay, loongson_delay);