#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: at91st.c,v 1.7 2020/07/03 16:23:02 maxv Exp $");
#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/time.h>
#include <sys/device.h>
#include <dev/clock_subr.h>
#include <sys/bus.h>
#include <machine/intr.h>
#include <arm/cpufunc.h>
#include <arm/at91/at91reg.h>
#include <arm/at91/at91var.h>
#include <arm/at91/at91streg.h>
#include <opt_hz.h>
#ifdef DEBUG_CLK
#define DPRINTF(fmt...) printf(fmt)
#else
#define DPRINTF(fmt...)
#endif
static int at91st_match(device_t, cfdata_t, void *);
static void at91st_attach(device_t, device_t, void *);
void rtcinit(void);
static int at91st_intr(void* arg);
struct at91st_softc {
bus_space_tag_t sc_iot;
bus_space_handle_t sc_ioh;
int sc_pid;
int sc_initialized;
};
static struct at91st_softc *at91st_sc = NULL;
static struct timeval lasttv;
#define AT91ST_DIVIDER (AT91_SCLK / HZ)
#define USEC_PER_TICK (1000000 / (AT91_SCLK / AT91ST_DIVIDER))
#if 0
static uint32_t at91st_count_to_usec(uint32_t count)
{
uint32_t result;
result = (1000000 * count) / AT91_SCLK;
if ((result * AT91_SCLK) != (count * 1000000))
{
result += 1;
}
return result;
}
static uint32_t usec_to_timer_count(uint32_t usec)
{
uint32_t result;
result = (AT91_SCLK * usec) / 1000000;
if ((result * 1000000) != (usec * AT91_SCLK))
{
result += 1;
}
return result;
}
#endif
#define READ_ST(offset) STREG(offset)
#define WRITE_ST(offset, value) do { \
STREG(offset) = (value); \
} while (0)
CFATTACH_DECL_NEW(at91st, sizeof(struct at91st_softc), at91st_match, at91st_attach, NULL, NULL);
static int
at91st_match(device_t parent, cfdata_t match, void *aux)
{
if (strcmp(match->cf_name, "at91st") == 0)
return 2;
return 0;
}
static void
at91st_attach(device_t parent, device_t self, void *aux)
{
struct at91st_softc *sc = device_private(self);
struct at91bus_attach_args *sa = aux;
printf("\n");
sc->sc_iot = sa->sa_iot;
sc->sc_pid = sa->sa_pid;
#if 0
DPRINTF("-> bus_space_map()\n");
if (bus_space_map(sc->sc_iot, sa->sa_addr, sa->sa_size, 0, &sc->sc_ioh) != 0)
panic("%s: Cannot map registers", device_xname(self));
#endif
if (at91st_sc == NULL)
at91st_sc = sc;
at91_peripheral_clock(sc->sc_pid, 1);
WRITE_ST(ST_IDR, -1);
WRITE_ST(ST_PIMR, AT91ST_DIVIDER);
WRITE_ST(ST_RTMR, 1);
sc->sc_initialized = 1;
DPRINTF("%s: done\n", __FUNCTION__);
}
static int
at91st_intr(void *arg)
{
if (READ_ST(ST_SR) & ST_SR_PITS)
{
hardclock((struct clockframe*) arg);
#if 0
if (getticks() % (HZ * 10) == 0)
printf("time %i sec\n", getticks()/HZ);
#endif
return 1;
}
else
{
return 0;
}
}
void
setstatclockrate(int hzz)
{
(void)hzz;
}
static void udelay(unsigned int usec);
void
cpu_initclocks(void)
{
struct at91st_softc *sc = at91st_sc;
if (!sc || !sc->sc_initialized)
panic("%s: driver has not been initialized! (sc=%p)", __FUNCTION__, sc);
stathz = profhz = 0;
WRITE_ST(ST_PIMR, AT91ST_DIVIDER);
at91_intr_establish(sc->sc_pid, IPL_CLOCK, INTR_HIGH_LEVEL, at91st_intr, NULL);
WRITE_ST(ST_IER, ST_SR_PITS);
}
void
microtime(register struct timeval *tvp)
{
u_int oldirqstate;
u_int current_count;
#ifdef DEBUG
if (at91st_sc == NULL) {
printf("microtime: called before initialize at91st\n");
tvp->tv_sec = 0;
tvp->tv_usec = 0;
return;
}
#endif
oldirqstate = disable_interrupts(I32_bit);
current_count = READ_ST(ST_CRTR);
*tvp = time;
#if 0
tvp->tv_usec += at91st_count_to_usec(AT91ST_DIVIDER - current_count);
while (__predict_false(tvp->tv_usec >= 1000000))
{
tvp->tv_usec -= 1000000;
tvp->tv_sec++;
}
#endif
if (__predict_false(tvp->tv_sec == lasttv.tv_sec && tvp->tv_usec <= lasttv.tv_usec))
{
tvp->tv_usec = lasttv.tv_usec + 1;
if (tvp->tv_usec >= 1000000)
{
tvp->tv_usec -= 1000000;
tvp->tv_sec++;
}
}
lasttv = *tvp;
restore_interrupts(oldirqstate);
}
#if 0
static void tdelay(unsigned int ticks)
{
uint32_t start, end, current;
current = getticks();
start = current;
end = start + ticks;
while (current < end)
current = getticks();
}
#endif
static void udelay(unsigned int usec)
{
uint32_t crtv, t, diff;
usec = (usec * 1000 + AT91_SCLK - 1) / AT91_SCLK + 1;
for (crtv = READ_ST(ST_CRTR);;) {
while (crtv == (t = READ_ST(ST_CRTR))) ;
diff = (t - crtv) & ST_CRTR_CRTV;
if (diff >= usec) {
break;
}
crtv = t;
usec -= diff;
}
}
void
delay(unsigned int usec)
{
#ifdef DEBUG
if (at91st_sc == NULL) {
printf("delay: called before start at91st\n");
return;
}
#endif
if (usec >= USEC_PER_TICK)
{
unsigned int ticks = usec / USEC_PER_TICK;
if (ticks*USEC_PER_TICK != usec)
ticks += 1;
while (ticks-- > 0) {
udelay(USEC_PER_TICK);
}
}
else
{
udelay(usec);
}
}