summaryrefslogtreecommitdiff
path: root/code/fe310/eos/timer.c
blob: 0a07ed9ddc1fa54fbcbd593fa94d355ffe8a266c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stddef.h>
#include <stdio.h>

#include "encoding.h"
#include "platform.h"
#include "event.h"
#include "timer.h"

#define MIN(X, Y)               (((X) < (Y)) ? (X) : (Y))
#define MAX(X, Y)               (((X) > (Y)) ? (X) : (Y))

static eos_timer_fptr_t timer_ext_handler = NULL;
volatile uint64_t timer_next = 0;
volatile uint64_t timer_next_evt = 0;

void handle_m_time_interrupt(void) {
    volatile uint64_t *mtime = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIME);
    volatile uint64_t *mtimecmp = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIMECMP);
    uint64_t now = *mtime;

    if (timer_next && (timer_next <= now)) {
        uint32_t next = timer_ext_handler();
        if (next) {
            timer_next = now + next;
        } else {
            timer_next = 0;
        }
    }
    if (timer_next_evt && (timer_next_evt <= now)) {
        eos_evtq_push(EOS_EVT_TIMER, NULL, 0);
        timer_next_evt = 0;
    }
    *mtimecmp = (timer_next && timer_next_evt) ? MIN(timer_next, timer_next_evt) : (timer_next ? timer_next : timer_next_evt);
    if (*mtimecmp == 0) clear_csr(mie, MIP_MTIP);
}

void eos_timer_init(void) {
    volatile uint64_t *mtimecmp = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIMECMP);
    *mtimecmp = 0;
    clear_csr(mie, MIP_MTIP);
}

void eos_timer_set(uint32_t tick, unsigned char is_evt) {
    volatile uint64_t *mtime = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIME);
    volatile uint64_t *mtimecmp = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIMECMP);

    clear_csr(mstatus, MSTATUS_MIE);

    uint64_t now = *mtime;
    uint64_t then = *mtimecmp;
    uint64_t next = now + tick;
    if (is_evt) {
        if ((timer_next_evt == 0) || (next < timer_next_evt)) timer_next_evt = next;
        next = timer_next ? MIN(timer_next, timer_next_evt) : timer_next_evt;
    } else if (timer_ext_handler) {
        if ((timer_next == 0) || (next < timer_next)) timer_next = next;
        next = timer_next_evt ? MIN(timer_next, timer_next_evt) : timer_next;
    }
    if ((then == 0) || (next < then)) *mtimecmp = next;
    if (then == 0) set_csr(mie, MIP_MTIP);

    set_csr(mstatus, MSTATUS_MIE);
}

void eos_timer_clear(unsigned char is_evt) {
    volatile uint64_t *mtimecmp = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIMECMP);

    clear_csr(mstatus, MSTATUS_MIE);

    if (is_evt) {
        timer_next_evt = 0;
        *mtimecmp = timer_next;
    } else {
        timer_next = 0;
        *mtimecmp = timer_next_evt;
    }
    if (*mtimecmp == 0) clear_csr(mie, MIP_MTIP);

    set_csr(mstatus, MSTATUS_MIE);
}

void eos_timer_set_handler(eos_timer_fptr_t handler) {
    clear_csr(mstatus, MSTATUS_MIE);
    timer_ext_handler = handler;
    set_csr(mstatus, MSTATUS_MIE);
}