-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
csr.h
61 lines (50 loc) · 1.81 KB
/
csr.h
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
#ifndef __CSR_H__
#define __CSR_H__
#include <stdint.h>
//-----------------------------------------------------------------
// Defines:
//-----------------------------------------------------------------
#define SR_SIE (1 << 1)
#define SR_MIE (1 << 3)
#define SR_SPIE (1 << 5)
#define SR_MPIE (1 << 7)
#define IRQ_S_SOFT 1
#define IRQ_M_SOFT 3
#define IRQ_S_TIMER 5
#define IRQ_M_TIMER 7
#define IRQ_S_EXT 9
#define IRQ_M_EXT 11
#define SR_IP_MSIP (1 << IRQ_M_SOFT)
#define SR_IP_MTIP (1 << IRQ_M_TIMER)
#define SR_IP_MEIP (1 << IRQ_M_EXT)
#define SR_IP_SSIP (1 << IRQ_S_SOFT)
#define SR_IP_STIP (1 << IRQ_S_TIMER)
#define SR_IP_SEIP (1 << IRQ_S_EXT)
#define MSTATUS_SIE 0x00000002
#define MSTATUS_MIE 0x00000008
#define MSTATUS_SPIE 0x00000020
#define MSTATUS_MPIE 0x00000080
#define MSTATUS_SPP 0x00000100
#define MSTATUS_MPP 0x00001800
#define MSTATUS_MPRV 0x00020000
#define MSTATUS_SUM 0x00040000
#define MSTATUS_MXR 0x00080000
//-----------------------------------------------------------------
// Helpers:
//-----------------------------------------------------------------
#define csr_read(reg) ({ unsigned long __tmp; \
asm volatile ("csrr %0, " #reg : "=r"(__tmp)); \
__tmp; })
#define csr_write(reg, val) ({ \
asm volatile ("csrw " #reg ", %0" :: "rK"(val)); })
#define csr_set(reg, bit) ({ unsigned long __tmp; \
asm volatile ("csrrs %0, " #reg ", %1" : "=r"(__tmp) : "rK"(bit)); \
__tmp; })
#define csr_clear(reg, bit) ({ unsigned long __tmp; \
asm volatile ("csrrc %0, " #reg ", %1" : "=r"(__tmp) : "rK"(bit)); \
__tmp; })
#define csr_swap(reg, val) ({ \
unsigned long __v = (unsigned long)(val); \
asm volatile ("csrrw %0, " #reg ", %1" : "=r" (__v) : "rK" (__v) : "memory"); \
__v; })
#endif