summaryrefslogtreecommitdiff
path: root/bsp/env/coreplexip-e31-arty/init.c
blob: 1f8b679105a3b2adadfdaee44c7bcdc03b2cb069 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//See LICENSE for license details.
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>

#include "platform.h"
#include "encoding.h"

#define CPU_FREQ 65000000
#define XSTR(x) #x
#define STR(x) XSTR(x)

#ifndef VECT_IRQ
  #define TRAP_ENTRY trap_entry
#else
  #define TRAP_ENTRY vtrap_entry
#endif

extern int main(int argc, char** argv);
extern void TRAP_ENTRY();

unsigned long get_cpu_freq()
{
  return CPU_FREQ;
}

unsigned long get_timer_freq()
{
  return get_cpu_freq();
}

uint64_t get_timer_value()
{
#if __riscv_xlen == 32
  while (1) {
    uint32_t hi = read_csr(mcycleh);
    uint32_t lo = read_csr(mcycle);
    if (hi == read_csr(mcycleh))
      return ((uint64_t)hi << 32) | lo;
  }
#else
  return read_csr(mcycle);
#endif
}

static void uart_init(size_t baud_rate)
{
  UART0_REG(UART_REG_DIV) = (get_cpu_freq() / 2) / baud_rate - 1;
  UART0_REG(UART_REG_TXCTRL) |= UART_TXEN;
}


#ifdef USE_PLIC
extern void handle_m_ext_interrupt();
#endif

#ifdef USE_M_TIME
extern void handle_m_time_interrupt();
#endif

#ifdef USE_LOCAL_ISR
typedef void (*my_interrupt_function_ptr_t) (void);
extern my_interrupt_function_ptr_t localISR[];
#endif

#ifndef VECT_IRQ
uintptr_t handle_trap(uintptr_t mcause, uintptr_t epc) __attribute__((noinline));
uintptr_t handle_trap(uintptr_t mcause, uintptr_t epc)
{
  if (0){
#ifdef USE_PLIC
    // External Machine-Level interrupt from PLIC
  } else if ((mcause & MCAUSE_INT) && ((mcause & MCAUSE_CAUSE) == IRQ_M_EXT)) {
    handle_m_ext_interrupt();
#endif
#ifdef USE_M_TIME
    // External Machine-Level interrupt from PLIC
  } else if ((mcause & MCAUSE_INT) && ((mcause & MCAUSE_CAUSE) == IRQ_M_TIMER)){
    handle_m_time_interrupt();
#endif
#ifdef USE_LOCAL_ISR
  } else if (mcause & MCAUSE_INT) {
    localISR[mcause & MCAUSE_CAUSE] ();
#endif
  }
  else {
    write(1, "Unhandled Trap:\n", 16);
    _exit(1 + mcause);
  }
  return epc;
}
#endif 

#ifdef USE_CLIC
void trap_entry(void) __attribute__((interrupt("SiFive-CLIC-preemptible"), aligned(64)));
void trap_entry(void)
{
  unsigned long mcause = read_csr(mcause);
  unsigned long mepc = read_csr(mepc);
  handle_trap(mcause, mepc);
}
#endif

void _init()
{
  #ifndef NO_INIT
  uart_init(115200);

  puts("core freq at " STR(CPU_FREQ) " Hz\n");

#ifdef USE_CLIC
  write_csr(mtvec, ((unsigned long)&trap_entry | MTVEC_CLIC));
#else
  write_csr(mtvec, ((unsigned long)&TRAP_ENTRY | MTVEC_VECTORED));
#endif

  #endif
}

void _fini()
{
}