5 #ifndef OPENTITAN_SW_DEVICE_LIB_BASE_CSR_H_
6 #define OPENTITAN_SW_DEVICE_LIB_BASE_CSR_H_
12 #include "sw/device/lib/base/csr_registers.h"
36 #ifndef OT_PLATFORM_RV32
49 #define CSR_FORCE_CONST_EXPR(x) asm volatile("" ::"i"(x))
51 uint32_t mock_csr_read(uint32_t addr);
53 #define CSR_READ_IMPL(csr, dest) \
55 static_assert(sizeof(*dest) == sizeof(uint32_t), \
56 "dest must point to a 4-byte variable"); \
57 CSR_FORCE_CONST_EXPR(csr); \
58 *dest = mock_csr_read(csr); \
61 void mock_csr_write(uint32_t addr, uint32_t value);
63 #define CSR_WRITE_IMPL(csr, val) \
65 static_assert(sizeof(val) == sizeof(uint32_t), \
66 "val must be a 4-byte value"); \
67 CSR_FORCE_CONST_EXPR(csr); \
68 mock_csr_write(csr, val); \
71 void mock_csr_set_bits(uint32_t addr, uint32_t mask);
73 #define CSR_SET_BITS_IMPL(csr, mask) \
75 static_assert(sizeof(mask) == sizeof(uint32_t), \
76 "mask must be a 4-byte value"); \
77 CSR_FORCE_CONST_EXPR(csr); \
78 mock_csr_set_bits(csr, mask); \
81 void mock_csr_clear_bits(uint32_t addr, uint32_t mask);
83 #define CSR_CLEAR_BITS_IMPL(csr, mask) \
85 static_assert(sizeof(mask) == sizeof(uint32_t), \
86 "mask must be a 4-byte value"); \
87 CSR_FORCE_CONST_EXPR(csr); \
88 mock_csr_clear_bits(csr, mask); \
93 #define CSR_READ_IMPL(csr, dest) \
95 static_assert(sizeof(*dest) == sizeof(uint32_t), \
96 "dest must point to a 4-byte variable"); \
97 asm volatile("csrr %0, %1;" : "=r"(*dest) : "i"(csr)); \
100 #define CSR_WRITE_IMPL(csr, val) \
102 static_assert(sizeof(val) == sizeof(uint32_t), \
103 "val must be a 4-byte value"); \
104 asm volatile("csrw %0, %1;" ::"i"(csr), "r"(val)); \
107 #define CSR_SET_BITS_IMPL(csr, mask) \
109 static_assert(sizeof(mask) == sizeof(uint32_t), \
110 "mask must be a 4-byte value"); \
111 asm volatile("csrs %0, %1;" ::"i"(csr), "r"(mask)); \
114 #define CSR_CLEAR_BITS_IMPL(csr, mask) \
116 static_assert(sizeof(mask) == sizeof(uint32_t), \
117 "mask must be a 4-byte value"); \
118 asm volatile("csrc %0, %1;" ::"i"(csr), "r"(mask)); \
135 #define CSR_READ(csr, dest) CSR_READ_IMPL(csr, dest)
147 #define CSR_WRITE(csr, val) CSR_WRITE_IMPL(csr, val)
159 #define CSR_SET_BITS(csr, mask) CSR_SET_BITS_IMPL(csr, mask)
171 #define CSR_CLEAR_BITS(csr, mask) CSR_CLEAR_BITS_IMPL(csr, mask)