summaryrefslogtreecommitdiff
path: root/bsp/ft2004/libraries/cpu/ft_aarch32_asm.h
blob: d39b221a2d4fe562737b7cf77f81b0be635d8ab6 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 * @ : Copyright (c) 2021 Phytium Information Technology, Inc.
 *
 * SPDX-License-Identifier: Apache-2.0.
 *
 * @Date: 2021-01-22 16:30:56
 * @LastEditTime: 2021-05-24 14:35:53
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \project_freertos\devices\ft2004\bsp\core\ft_asm.h
 */

#ifndef FT_AARCH32_ASM_H
#define FT_AARCH32_ASM_H

#ifdef __cplusplus
extern "C"
{
#endif

#include "ft_types.h"

#define __ASM __asm
#define __STRINGIFY(x) #x
/* C语言实现MCR指令 */
#define __MCR(coproc, opcode_1, src, CRn, CRm, opcode_2)                                                                                                   \
    __ASM volatile("MCR " __STRINGIFY(p##coproc) ", " __STRINGIFY(opcode_1) ", "                                                                           \
                                                                            "%0, " __STRINGIFY(c##CRn) ", " __STRINGIFY(c##CRm) ", " __STRINGIFY(opcode_2) \
                   :                                                                                                                                       \
                   : "r"(src)                                                                                                                              \
                   : "memory");

/* C语言实现MRC指令 */
#define __MRC(coproc, opcode_1, CRn, CRm, opcode_2)                                                                                                            \
    ({                                                                                                                                                         \
        u32 __dst;                                                                                                                                             \
        __ASM volatile("MRC " __STRINGIFY(p##coproc) ", " __STRINGIFY(opcode_1) ", "                                                                           \
                                                                                "%0, " __STRINGIFY(c##CRn) ", " __STRINGIFY(c##CRm) ", " __STRINGIFY(opcode_2) \
                       : "=r"(__dst)::"memory");                                                                                                               \
        __dst;                                                                                                                                                 \
    })

    __attribute__((always_inline)) __STATIC_INLINE u32 __get_VBAR(void)
    {
        return __MRC(15, 0, 12, 0, 0);
    }

    __attribute__((always_inline)) __STATIC_INLINE void __set_VBAR(u32 vbar)
    {
        __MCR(15, 0, vbar, 12, 0, 0);
    }

    __attribute__((always_inline)) __STATIC_INLINE void sys_icc_igrpen0_set(u32 value)
    {
        __MCR(15, 0, value, 12, 12, 6);
    }

    __attribute__((always_inline)) __STATIC_INLINE u32 sys_icc_igrpen0_get(void)
    {
        return __MRC(15, 0, 12, 12, 6);
    }

    __attribute__((always_inline)) __STATIC_INLINE void sys_icc_igrpen1_set(u32 value)
    {
        __MCR(15, 0, value, 12, 12, 7);
    }

    __attribute__((always_inline)) __STATIC_INLINE u32 sys_icc_igrpen1_get(void)
    {
        return __MRC(15, 0, 12, 12, 7);
    }

    __attribute__((always_inline)) __STATIC_INLINE void sys_icc_ctlr_set(u32 value)
    {
        __MCR(15, 0, value, 12, 12, 4);
    }

    __attribute__((always_inline)) __STATIC_INLINE u32 sys_icc_ctlr_get(void)
    {
        return __MRC(15, 0, 12, 12, 4);
    }

    __attribute__((always_inline)) __STATIC_INLINE u32 sys_icc_hppir0_get(void)
    {
        return __MRC(15, 0, 12, 8, 2);
    }

    __attribute__((always_inline)) __STATIC_INLINE void sys_icc_bpr_set(u32 value)
    {
        __MCR(15, 0, value, 12, 12, 3);
    }

    __attribute__((always_inline)) __STATIC_INLINE u32 sys_icc_bpr_get(void)
    {
        return __MRC(15, 0, 12, 12, 3);
    }

    __attribute__((always_inline)) __STATIC_INLINE u32 sys_icc_hppir1_get(void)
    {
        return __MRC(15, 0, 12, 12, 2);
    }

    __attribute__((always_inline)) __STATIC_INLINE void sys_icc_eoir0_set(u32 value)
    {
        __MCR(15, 0, value, 12, 8, 1);
    }

    __attribute__((always_inline)) __STATIC_INLINE void sys_icc_eoir1_set(u32 value)
    {
        __MCR(15, 0, value, 12, 12, 1);
    }

    __attribute__((always_inline)) __STATIC_INLINE void sys_icc_pmr_set(u32 value)
    {
        __MCR(15, 0, value, 4, 6, 0);
    }

    __attribute__((always_inline)) __STATIC_INLINE u32 sys_icc_pmr_get(void)
    {
        return __MRC(15, 0, 4, 6, 0);
    }

    __attribute__((always_inline)) __STATIC_INLINE u32 sys_icc_iar1_get(void)
    {
        return __MRC(15, 0, 12, 12, 0);
    }

    __attribute__((always_inline)) __STATIC_INLINE void sys_icc_sre_set(u32 value)
    {
        __MCR(15, 0, value, 12, 12, 5);
    }

    __attribute__((always_inline)) __STATIC_INLINE u32 sys_icc_sre_get(void)
    {
        return __MRC(15, 0, 12, 12, 5);
    }

    __attribute__((always_inline)) __STATIC_INLINE u32 sys_icc_rpr_get(void)
    {
        return __MRC(15, 0, 12, 11, 3);
    }

    /*   Generic Timer registers  */
    /**
     * @name: arm_aarch32_cntfrq_get
     * @msg:  This register is provided so that software can discover the frequency of the system counter.
     * @return {__STATIC_INLINEu32}:  frequency of the system counter
     */
    __attribute__((always_inline)) __STATIC_INLINE u32 arm_aarch32_cntfrq_get(void)
    {
        return __MRC(15, 0, 14, 0, 0);
    }

    /**
     * @name: arm_aarch32_cnthv_tval_get
     * @msg:  Provides AArch32 access to the timer value for the EL2 virtual timer.
     * @return {__STATIC_INLINEu32}: EL2 virtual timer Cnt.
     */
    __attribute__((always_inline)) __STATIC_INLINE u32 arm_aarch32_cnthv_tval_get(void)
    {
        return __MRC(15, 0, 14, 3, 0);
    }

    /**
     * @name: arm_aarch32_cnthv_ctl_set
     * @msg:  Provides AArch32 access to the control register for the EL2 virtual timer.
     * @in param {u32}: RegValue;ENABLE: bit [0] 0 Timer disabled,1 Timer enabled.
     *                  IMASK,bit [1]: 0 Timer interrupt is not masked by the IMASK bit.   1 Timer interrupt is masked by the IMASK bit.
     *                  ISTATUS, bit [2]:  0 Timer condition is not met. 1 Timer condition is met. rea-only
     */
    __attribute__((always_inline)) __STATIC_INLINE void arm_aarch32_cnthv_ctl_set(u32 RegValue)
    {
        __MCR(15, 0, RegValue, 14, 3, 1);
    }

    /**
     * @name: arm_aarch32_cnthv_ctl_get
     * @msg:  Provides AArch32 access to the control register for the EL2 virtual timer.
     * @return {__STATIC_INLINEu32}: RegValue;ENABLE: bit [0] 0 Timer disabled,1 Timer enabled.
     *                  IMASK,bit [1]: 0 Timer interrupt is not masked by the IMASK bit.   1 Timer interrupt is masked by the IMASK bit.
     *                  ISTATUS, bit [2]:  0 Timer condition is not met. 1 Timer condition is met. read-only
     */
    __attribute__((always_inline)) __STATIC_INLINE u32 arm_aarch32_cnthv_ctl_get(void)
    {
        return __MRC(15, 0, 14, 3, 1);
    }

    /**
     * @name: arm_aarch32_cnthv_tval_set
     * @msg:  Provides AArch32 access to the timer value for the EL2 virtual timer.
     * @in param {u32}: TimerValue, bits [31:0] The TimerValue view of the EL2 virtual timer.
     */
    __attribute__((always_inline)) __STATIC_INLINE void arm_aarch32_cnthv_tval_set(u32 RegValue)
    {
        __MCR(15, 0, RegValue, 14, 3, 0);
    }

    /**
     * @name: arm_aarch32_cntvct_get
     * @msg:  Read the register that holds the 64-bit virtual count value. The virtual count value is equal to the physical count value visible in CNTPCT minus the virtual offset visible in CNTVOFF.
     * @return {__STATIC_INLINEu64}Bits [63:0] Virtual count value.
     */
    __attribute__((always_inline)) __STATIC_INLINE u64 arm_aarch32_cntvct_get(void)
    {
        /* "r0" --- low,
        "r1" --- hi
      */
        u32 low;
        u32 hi;
        __asm__ volatile(
            ".word 0xec510f1e \n" /* mrrc p15, 1, r0, r1, c14 */
            "mov %0, r0 \n"
            "mov %1, r1 \n"
            : "=&r"(low), "=&r"(hi));
        return (((u64)hi) << 32) | low;
    }

    /* physical */

    /**
     * @name: arm_aarch32_cntp_tval_get
     * @msg:  Read the register that holds the timer value for the EL1 physical timer.
     * @return {__STATIC_INLINEu32}: TimerValue, bits [31:0] The TimerValue view of the EL1 physical timer.
     */
    __attribute__((always_inline)) __STATIC_INLINE u32 arm_aarch32_cntp_tval_get(void)
    {
        return __MRC(15, 0, 14, 2, 0);
    }

    /**
     * @name: arm_aarch32_cntp_tval_set
     * @msg:  write the register that control register for the EL1 physical timer.
     * @in param {u32}: TimerValue, bits [31:0] The TimerValue view of the EL1 physical timer.
     */
    __attribute__((always_inline)) __STATIC_INLINE void arm_aarch32_cntp_tval_set(u32 RegValue)
    {
        __MCR(15, 0, RegValue, 14, 2, 0);
    }

    /**
     * @name: arm_aarch32_cntp_ctl_set
     * @msg:  write the register that control register for the EL1 physical timer.
     * @in param {u32}: ENABLE, bit[0] Enables the timer ; IMASK, bit [1] Timer interrupt mask bit; ISTATUS, bit [2] The status of the timer.
     */
    __attribute__((always_inline)) __STATIC_INLINE void arm_aarch32_cntp_ctl_set(u32 RegValue)
    {
        __MCR(15, 0, RegValue, 14, 2, 1);
    }

    /**
     * @name: arm_aarch32_cntp_ctl_get
     * @msg:  Read the register that control register for the EL1 physical timer.
     * @return {__STATIC_INLINEu32}: ENABLE, bit[0] Enables the timer ; IMASK, bit [1] Timer interrupt mask bit; ISTATUS, bit [2] The status of the timer.
     */
    __attribute__((always_inline)) __STATIC_INLINE u32 arm_aarch32_cntp_ctl_get(void)
    {
        return __MRC(15, 0, 14, 2, 1);
    }

    /**
     * @name: arm_aarch32_cntpct_get
     * @msg:  Read the register that holds the 64-bit physical count value.
     * @return {__STATIC_INLINEu64} CompareValue, bits [63:0] Physical count value.
     */
    __attribute__((always_inline)) __STATIC_INLINE u64 arm_aarch32_cntpct_get(void)
    {
        /* "r0" --- low,
        "r1" --- hi
      */
        u32 low;
        u32 hi;
        __asm__ volatile(

            ".word 0xec510f0e \n" /* mrrc p15, 0, r0, r1, c14 */
            "mov %0, r0 \n"
            "mov %1, r1 \n"
            : "=&r"(low), "=&r"(hi));
        return (((u64)hi) << 32) | low;
    }

#define IRQ_DISABLE()               \
    __asm volatile("CPSID i" ::     \
                       : "memory"); \
    __asm volatile("DSB");          \
    __asm volatile("ISB");

#define IRQ_ENABLE()                \
    __asm volatile("CPSIE i" ::     \
                       : "memory"); \
    __asm volatile("DSB");          \
    __asm volatile("ISB");

    /* the exception stack without VFP registers */
    struct ft_hw_exp_stack
    {
        u32 r0;
        u32 r1;
        u32 r2;
        u32 r3;
        u32 r4;
        u32 r5;
        u32 r6;
        u32 r7;
        u32 r8;
        u32 r9;
        u32 r10;
        u32 fp;
        u32 ip;
        u32 sp;
        u32 lr;
        u32 pc;
        u32 cpsr;
    };

#ifdef __cplusplus
}
#endif

#endif // !