|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 Nordic Semiconductor ASA |
| 3 | + * Copyright (c) 2025 Siemens Mobility GmbH |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: Apache-2.0 |
| 6 | + */ |
| 7 | + |
| 8 | +#include <stdint.h> |
| 9 | +#include <zephyr/irq.h> |
| 10 | +#include <zephyr/sys/util_macro.h> |
| 11 | +#include <zephyr/toolchain.h> |
| 12 | + |
| 13 | +#ifdef CONFIG_ARM_CUSTOM_INTERRUPT_CONTROLLER |
| 14 | +extern void z_soc_irq_eoi(unsigned int irq); |
| 15 | +#else |
| 16 | +#include <zephyr/drivers/interrupt_controller/gic.h> |
| 17 | +#endif |
| 18 | + |
| 19 | +#define READ_COPROCESSOR_REGISTER(out, coproc, opc1, crn, crm, opc2) \ |
| 20 | + __asm__ volatile("mrc " #coproc ", " #opc1 ", %0, " #crn ", " #crm ", " #opc2 "\n" : "=r" (out) ::); |
| 21 | + |
| 22 | +#define WRITE_COPROCESSOR_REGISTER(in, coproc, opc1, crn, crm, opc2) \ |
| 23 | + __asm__ volatile("mcr " #coproc ", " #opc1 ", %0, " #crn ", " #crm ", " #opc2 "\n" :: "r" (in) :) |
| 24 | + |
| 25 | +void cleanup_arm_interrupts(void) |
| 26 | +{ |
| 27 | + /* Allow any pending interrupts to be recognized */ |
| 28 | + __ISB(); |
| 29 | + __disable_irq(); |
| 30 | + |
| 31 | + for (unsigned int i = 0; i < CONFIG_NUM_IRQS; ++i) { |
| 32 | + irq_disable(i); |
| 33 | + } |
| 34 | + |
| 35 | + for (unsigned int i = 0; i < CONFIG_NUM_IRQS; ++i) { |
| 36 | +#ifdef CONFIG_ARM_CUSTOM_INTERRUPT_CONTROLLER |
| 37 | + z_soc_irq_eoi(i); |
| 38 | +#else |
| 39 | + arm_gic_eoi(i); |
| 40 | +#endif /* CONFIG_ARM_CUSTOM_INTERRUPT_CONTROLLER */ |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +#if CONFIG_CPU_HAS_ARM_MPU |
| 45 | +__weak void z_arm_clear_arm_mpu_config(void) |
| 46 | +{ |
| 47 | + uint8_t i; |
| 48 | + uint8_t num_regions; |
| 49 | + uint32_t mpu_type_register; |
| 50 | + |
| 51 | + /* Disable MPU */ |
| 52 | + uint32_t val; |
| 53 | + READ_COPROCESSOR_REGISTER(val, p15, 0, c1, c0, 0); |
| 54 | + val &= ~BIT(0); |
| 55 | + __DSB(); |
| 56 | + |
| 57 | + WRITE_COPROCESSOR_REGISTER(val, p15, 0, c1, c0, 0); |
| 58 | + __ISB(); |
| 59 | + |
| 60 | + /* The number of MPU regions is stored in bits 15:8 of the MPU type register */ |
| 61 | + READ_COPROCESSOR_REGISTER(mpu_type_register, p15, 0, c0, c0, 4); |
| 62 | + num_regions = (uint8_t) ((mpu_type_register >> 8) & BIT_MASK(8)); |
| 63 | + |
| 64 | + for (i = 0; i < num_regions; ++i) { |
| 65 | + /* Select region in the MPU and clear the region size field */ |
| 66 | + WRITE_COPROCESSOR_REGISTER(i, p15, 0, c6, c2, 0); |
| 67 | + WRITE_COPROCESSOR_REGISTER(0, p15, 0, c6, c1, 2); |
| 68 | + } |
| 69 | +} |
| 70 | +#endif |
0 commit comments