2012-03-17 17:32:52 -07:00
|
|
|
/*
|
2015-06-03 17:44:46 -07:00
|
|
|
* Copyright (c) 2012-2015 Travis Geiselbrecht
|
2012-03-17 17:32:52 -07:00
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
|
* a copy of this software and associated documentation files
|
|
|
|
|
* (the "Software"), to deal in the Software without restriction,
|
|
|
|
|
* including without limitation the rights to use, copy, modify, merge,
|
|
|
|
|
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
|
* and to permit persons to whom the Software is furnished to do so,
|
|
|
|
|
* subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
*/
|
2019-06-17 18:28:51 -07:00
|
|
|
#include <lk/debug.h>
|
2012-03-17 17:32:52 -07:00
|
|
|
#include <arch.h>
|
|
|
|
|
#include <arch/ops.h>
|
|
|
|
|
#include <arch/arm.h>
|
2013-03-10 17:41:10 -07:00
|
|
|
#include <kernel/thread.h>
|
2013-07-19 18:52:28 -07:00
|
|
|
#include <kernel/debug.h>
|
2012-03-17 17:32:52 -07:00
|
|
|
#include <platform.h>
|
2012-11-11 20:08:50 -08:00
|
|
|
#include <arch/arm/cm.h>
|
2015-12-14 13:15:42 -08:00
|
|
|
#include <target.h>
|
2012-04-10 14:19:16 -07:00
|
|
|
|
|
|
|
|
extern void *vectab;
|
2012-03-17 17:32:52 -07:00
|
|
|
|
2014-03-05 12:51:34 -08:00
|
|
|
#if ARM_CM_DYNAMIC_PRIORITY_SIZE
|
2013-03-10 17:19:53 -07:00
|
|
|
unsigned int arm_cm_num_irq_pri_bits;
|
|
|
|
|
unsigned int arm_cm_irq_pri_mask;
|
2012-10-27 16:31:28 -07:00
|
|
|
#endif
|
2012-04-10 14:19:16 -07:00
|
|
|
|
2012-03-17 17:32:52 -07:00
|
|
|
void arch_early_init(void)
|
|
|
|
|
{
|
2014-03-05 12:51:34 -08:00
|
|
|
|
2015-11-09 14:27:09 -08:00
|
|
|
arch_disable_ints();
|
2012-04-10 14:19:16 -07:00
|
|
|
|
2016-03-28 19:57:17 -07:00
|
|
|
#if (__CORTEX_M >= 0x03) || (CORTEX_SC >= 300)
|
2016-01-12 11:19:33 -08:00
|
|
|
uint i;
|
2015-11-09 14:27:09 -08:00
|
|
|
/* set the vector table base */
|
|
|
|
|
SCB->VTOR = (uint32_t)&vectab;
|
2012-04-10 14:19:16 -07:00
|
|
|
|
2014-03-05 12:51:34 -08:00
|
|
|
#if ARM_CM_DYNAMIC_PRIORITY_SIZE
|
2015-11-09 14:27:09 -08:00
|
|
|
/* number of priorities */
|
|
|
|
|
for (i=0; i < 7; i++) {
|
|
|
|
|
__set_BASEPRI(1 << i);
|
|
|
|
|
if (__get_BASEPRI() != 0)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
arm_cm_num_irq_pri_bits = 8 - i;
|
|
|
|
|
arm_cm_irq_pri_mask = ~((1 << i) - 1) & 0xff;
|
2012-10-27 16:31:28 -07:00
|
|
|
#endif
|
2012-04-10 14:19:16 -07:00
|
|
|
|
2015-11-09 14:27:09 -08:00
|
|
|
/* clear any pending interrupts and set all the vectors to medium priority */
|
|
|
|
|
uint groups = (SCnSCB->ICTR & 0xf) + 1;
|
|
|
|
|
for (i = 0; i < groups; i++) {
|
|
|
|
|
NVIC->ICER[i] = 0xffffffff;
|
|
|
|
|
NVIC->ICPR[i] = 0xffffffff;
|
|
|
|
|
for (uint j = 0; j < 32; j++) {
|
|
|
|
|
NVIC_SetPriority(i*32 + j, arm_cm_medium_priority());
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-05 12:51:34 -08:00
|
|
|
|
2015-11-09 14:27:09 -08:00
|
|
|
/* leave BASEPRI at 0 */
|
|
|
|
|
__set_BASEPRI(0);
|
2012-04-10 14:19:16 -07:00
|
|
|
|
2015-11-09 14:27:09 -08:00
|
|
|
/* set priority grouping to 0 */
|
|
|
|
|
NVIC_SetPriorityGrouping(0);
|
2012-04-10 14:19:16 -07:00
|
|
|
|
2015-11-09 14:27:09 -08:00
|
|
|
/* enable certain faults */
|
|
|
|
|
SCB->SHCSR |= (SCB_SHCSR_USGFAULTENA_Msk | SCB_SHCSR_BUSFAULTENA_Msk | SCB_SHCSR_MEMFAULTENA_Msk);
|
2012-04-10 14:19:16 -07:00
|
|
|
|
2015-11-09 14:27:09 -08:00
|
|
|
/* set the svc and pendsv priority level to pretty low */
|
2016-01-12 11:19:33 -08:00
|
|
|
#endif
|
2015-11-09 14:27:09 -08:00
|
|
|
NVIC_SetPriority(SVCall_IRQn, arm_cm_lowest_priority());
|
|
|
|
|
NVIC_SetPriority(PendSV_IRQn, arm_cm_lowest_priority());
|
2012-05-10 18:59:29 -07:00
|
|
|
|
2015-11-09 14:27:09 -08:00
|
|
|
/* set systick and debugmonitor to medium priority */
|
|
|
|
|
NVIC_SetPriority(SysTick_IRQn, arm_cm_medium_priority());
|
2016-01-18 11:07:31 -08:00
|
|
|
|
|
|
|
|
#if (__CORTEX_M >= 0x03)
|
2015-11-09 14:27:09 -08:00
|
|
|
NVIC_SetPriority(DebugMonitor_IRQn, arm_cm_medium_priority());
|
2016-01-18 11:07:31 -08:00
|
|
|
#endif
|
2015-07-10 00:50:05 -07:00
|
|
|
|
2015-09-22 14:39:11 -07:00
|
|
|
/* FPU settings ------------------------------------------------------------*/
|
|
|
|
|
#if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
|
|
|
|
|
SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
|
|
|
|
|
#endif
|
|
|
|
|
|
2015-07-10 00:50:05 -07:00
|
|
|
#if ARM_WITH_CACHE
|
2015-11-09 14:27:09 -08:00
|
|
|
arch_enable_cache(UCACHE);
|
2015-07-10 00:50:05 -07:00
|
|
|
#endif
|
2012-03-17 17:32:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void arch_init(void)
|
|
|
|
|
{
|
2012-05-30 20:25:51 -07:00
|
|
|
#if ENABLE_CYCLE_COUNTER
|
2019-06-12 14:25:39 -07:00
|
|
|
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
|
|
|
|
|
DWT->CYCCNT = 0;
|
|
|
|
|
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; // enable cycle counter
|
2012-05-30 20:25:51 -07:00
|
|
|
#endif
|
2015-11-11 13:22:07 -08:00
|
|
|
printf("CONTROL 0x%x\n", __get_CONTROL());
|
2016-03-28 19:57:17 -07:00
|
|
|
#if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
|
2015-11-11 13:22:07 -08:00
|
|
|
printf("FPSCR 0x%x\n", __get_FPSCR());
|
|
|
|
|
printf("FPCCR 0x%x\n", FPU->FPCCR);
|
2016-03-28 19:57:17 -07:00
|
|
|
#endif
|
2012-03-17 17:32:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void arch_quiesce(void)
|
|
|
|
|
{
|
2016-03-09 15:21:04 -08:00
|
|
|
#if ARM_WITH_CACHE
|
|
|
|
|
arch_disable_cache(UCACHE);
|
|
|
|
|
#endif
|
2012-03-17 17:32:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void arch_idle(void)
|
|
|
|
|
{
|
2015-11-09 14:27:09 -08:00
|
|
|
__asm__ volatile("wfi");
|
2012-03-17 17:32:52 -07:00
|
|
|
}
|
|
|
|
|
|
2016-01-12 11:19:33 -08:00
|
|
|
#if (__CORTEX_M >= 0x03) || (CORTEX_SC >= 300)
|
|
|
|
|
|
2013-03-10 17:19:53 -07:00
|
|
|
void _arm_cm_set_irqpri(uint32_t pri)
|
2012-04-10 14:19:16 -07:00
|
|
|
{
|
2015-11-09 14:27:09 -08:00
|
|
|
if (pri == 0) {
|
|
|
|
|
__disable_irq(); // cpsid i
|
|
|
|
|
__set_BASEPRI(0);
|
|
|
|
|
} else if (pri >= 256) {
|
|
|
|
|
__set_BASEPRI(0);
|
|
|
|
|
__enable_irq();
|
|
|
|
|
} else {
|
|
|
|
|
uint32_t _pri = pri & arm_cm_irq_pri_mask;
|
|
|
|
|
|
|
|
|
|
if (_pri == 0)
|
|
|
|
|
__set_BASEPRI(1 << (8 - arm_cm_num_irq_pri_bits));
|
|
|
|
|
else
|
|
|
|
|
__set_BASEPRI(_pri);
|
|
|
|
|
__enable_irq(); // cpsie i
|
|
|
|
|
}
|
2012-04-10 14:19:16 -07:00
|
|
|
}
|
2016-01-12 11:19:33 -08:00
|
|
|
#endif
|
|
|
|
|
|
2012-04-10 14:19:16 -07:00
|
|
|
|
2013-03-10 17:41:10 -07:00
|
|
|
void arm_cm_irq_entry(void)
|
|
|
|
|
{
|
2015-11-09 14:27:09 -08:00
|
|
|
// Set PRIMASK to 1
|
|
|
|
|
// This is so that later calls to arch_ints_disabled() returns true while we're inside the int handler
|
|
|
|
|
// Note: this will probably screw up future efforts to stack higher priority interrupts since we're setting
|
|
|
|
|
// the cpu to essentially max interrupt priority here. Will have to rethink it then.
|
|
|
|
|
__disable_irq();
|
|
|
|
|
|
|
|
|
|
THREAD_STATS_INC(interrupts);
|
|
|
|
|
KEVLOG_IRQ_ENTER(__get_IPSR());
|
2015-12-14 13:15:42 -08:00
|
|
|
|
|
|
|
|
target_set_debug_led(1, true);
|
2013-03-10 17:41:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void arm_cm_irq_exit(bool reschedule)
|
|
|
|
|
{
|
2015-12-14 13:15:42 -08:00
|
|
|
target_set_debug_led(1, false);
|
|
|
|
|
|
2015-11-09 14:27:09 -08:00
|
|
|
if (reschedule)
|
|
|
|
|
arm_cm_trigger_preempt();
|
2013-03-10 17:41:10 -07:00
|
|
|
|
2015-11-09 14:27:09 -08:00
|
|
|
KEVLOG_IRQ_EXIT(__get_IPSR());
|
2015-06-03 17:44:46 -07:00
|
|
|
|
2015-11-09 14:27:09 -08:00
|
|
|
__enable_irq(); // clear PRIMASK
|
2013-03-10 17:41:10 -07:00
|
|
|
}
|
|
|
|
|
|
2014-11-07 13:25:42 -08:00
|
|
|
void arch_chain_load(void *entry, ulong arg0, ulong arg1, ulong arg2, ulong arg3)
|
2014-08-12 16:10:16 -07:00
|
|
|
{
|
2016-03-09 15:21:04 -08:00
|
|
|
#if (__CORTEX_M >= 0x03)
|
|
|
|
|
|
|
|
|
|
uint32_t *vectab = (uint32_t *)entry;
|
|
|
|
|
|
|
|
|
|
__asm__ volatile(
|
|
|
|
|
"mov r0, %[arg0]; "
|
|
|
|
|
"mov r1, %[arg1]; "
|
|
|
|
|
"mov r2, %[arg2]; "
|
|
|
|
|
"mov r3, %[arg3]; "
|
|
|
|
|
"mov sp, %[SP]; "
|
|
|
|
|
"bx %[entry]; "
|
|
|
|
|
:
|
|
|
|
|
: [arg0]"r"(arg0),
|
|
|
|
|
[arg1]"r"(arg1),
|
|
|
|
|
[arg2]"r"(arg2),
|
|
|
|
|
[arg3]"r"(arg3),
|
|
|
|
|
[SP]"r"(vectab[0]),
|
|
|
|
|
[entry]"r"(vectab[1])
|
|
|
|
|
: "r0", "r1", "r2", "r3"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
__UNREACHABLE;
|
|
|
|
|
#else
|
2014-08-12 16:10:16 -07:00
|
|
|
PANIC_UNIMPLEMENTED;
|
2016-03-09 15:21:04 -08:00
|
|
|
#endif
|
2014-08-12 16:10:16 -07:00
|
|
|
}
|