blob: dddfbf532def4665e614259575ba0b363a6bb57e [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2012-2015 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#include <debug.h>
24#include <arch.h>
25#include <arch/ops.h>
26#include <arch/arm.h>
27#include <kernel/thread.h>
28#include <kernel/debug.h>
29#include <platform.h>
30#include <arch/arm/cm.h>
31#include <target.h>
32
33extern void *vectab;
34
35#if ARM_CM_DYNAMIC_PRIORITY_SIZE
36unsigned int arm_cm_num_irq_pri_bits;
37unsigned int arm_cm_irq_pri_mask;
38#endif
39
40void arch_early_init(void)
41{
42
43 arch_disable_ints();
44
45#if (__CORTEX_M >= 0x03) || (CORTEX_SC >= 300)
46 uint i;
47 /* set the vector table base */
48 SCB->VTOR = (uint32_t)&vectab;
49
50#if ARM_CM_DYNAMIC_PRIORITY_SIZE
51 /* number of priorities */
52 for (i=0; i < 7; i++) {
53 __set_BASEPRI(1 << i);
54 if (__get_BASEPRI() != 0)
55 break;
56 }
57 arm_cm_num_irq_pri_bits = 8 - i;
58 arm_cm_irq_pri_mask = ~((1 << i) - 1) & 0xff;
59#endif
60
61 /* clear any pending interrupts and set all the vectors to medium priority */
62 uint groups = (SCnSCB->ICTR & 0xf) + 1;
63 for (i = 0; i < groups; i++) {
64 NVIC->ICER[i] = 0xffffffff;
65 NVIC->ICPR[i] = 0xffffffff;
66 for (uint j = 0; j < 32; j++) {
67 NVIC_SetPriority(i*32 + j, arm_cm_medium_priority());
68 }
69 }
70
71 /* leave BASEPRI at 0 */
72 __set_BASEPRI(0);
73
74 /* set priority grouping to 0 */
75 NVIC_SetPriorityGrouping(0);
76
77 /* enable certain faults */
78 SCB->SHCSR |= (SCB_SHCSR_USGFAULTENA_Msk | SCB_SHCSR_BUSFAULTENA_Msk | SCB_SHCSR_MEMFAULTENA_Msk);
79
80 /* set the svc and pendsv priority level to pretty low */
81#endif
82 NVIC_SetPriority(SVCall_IRQn, arm_cm_lowest_priority());
83 NVIC_SetPriority(PendSV_IRQn, arm_cm_lowest_priority());
84
85 /* set systick and debugmonitor to medium priority */
86 NVIC_SetPriority(SysTick_IRQn, arm_cm_medium_priority());
87
88#if (__CORTEX_M >= 0x03)
89 NVIC_SetPriority(DebugMonitor_IRQn, arm_cm_medium_priority());
90#endif
91
92#if ARM_WITH_CACHE
93 arch_enable_cache(UCACHE);
94#endif
95}
96
97void arch_init(void)
98{
99#if ENABLE_CYCLE_COUNTER
100 *REG32(SCB_DEMCR) |= 0x01000000; // global trace enable
101 *REG32(DWT_CYCCNT) = 0;
102 *REG32(DWT_CTRL) |= 1; // enable cycle counter
103#endif
104}
105
106void arch_quiesce(void)
107{
108}
109
110void arch_idle(void)
111{
112 __asm__ volatile("wfi");
113}
114
115#if (__CORTEX_M >= 0x03) || (CORTEX_SC >= 300)
116
117void _arm_cm_set_irqpri(uint32_t pri)
118{
119 if (pri == 0) {
120 __disable_irq(); // cpsid i
121 __set_BASEPRI(0);
122 } else if (pri >= 256) {
123 __set_BASEPRI(0);
124 __enable_irq();
125 } else {
126 uint32_t _pri = pri & arm_cm_irq_pri_mask;
127
128 if (_pri == 0)
129 __set_BASEPRI(1 << (8 - arm_cm_num_irq_pri_bits));
130 else
131 __set_BASEPRI(_pri);
132 __enable_irq(); // cpsie i
133 }
134}
135#endif
136
137
138void arm_cm_irq_entry(void)
139{
140 // Set PRIMASK to 1
141 // This is so that later calls to arch_ints_disabled() returns true while we're inside the int handler
142 // Note: this will probably screw up future efforts to stack higher priority interrupts since we're setting
143 // the cpu to essentially max interrupt priority here. Will have to rethink it then.
144 __disable_irq();
145
146 THREAD_STATS_INC(interrupts);
147 KEVLOG_IRQ_ENTER(__get_IPSR());
148
149 target_set_debug_led(1, true);
150}
151
152void arm_cm_irq_exit(bool reschedule)
153{
154 target_set_debug_led(1, false);
155
156 if (reschedule)
157 arm_cm_trigger_preempt();
158
159 KEVLOG_IRQ_EXIT(__get_IPSR());
160
161 __enable_irq(); // clear PRIMASK
162}
163
164void arch_chain_load(void *entry, ulong arg0, ulong arg1, ulong arg2, ulong arg3)
165{
166 PANIC_UNIMPLEMENTED;
167}