1 /*
2  * Copyright (c) 2024 Michael Hope
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT wch_pfic
8 
9 #include <ch32fun.h>
10 
11 #include <zephyr/arch/cpu.h>
12 #include <zephyr/init.h>
13 #include <zephyr/irq.h>
14 #include <zephyr/kernel.h>
15 
16 #define SEVONPEND (1 << 4)
17 #define WFITOWFE  (1 << 3)
18 
arch_irq_enable(unsigned int irq)19 void arch_irq_enable(unsigned int irq)
20 {
21 	PFIC->IENR[irq / 32] = 1 << (irq % 32);
22 }
23 
arch_irq_disable(unsigned int irq)24 void arch_irq_disable(unsigned int irq)
25 {
26 	PFIC->IRER[irq / 32] |= 1 << (irq % 32);
27 }
28 
arch_irq_is_enabled(unsigned int irq)29 int arch_irq_is_enabled(unsigned int irq)
30 {
31 	return ((PFIC->ISR[irq >> 5] & (1 << (irq & 0x1F))) != 0);
32 }
33 
pfic_init(void)34 static int pfic_init(void)
35 {
36 	/* `wfi` is called with interrupts disabled. Configure the PFIC to wake up on any event,
37 	 * including any interrupt.
38 	 */
39 	PFIC->SCTLR = SEVONPEND | WFITOWFE;
40 	return 0;
41 }
42 
43 SYS_INIT(pfic_init, PRE_KERNEL_1, CONFIG_INTC_INIT_PRIORITY);
44