1 /*
2  * Copyright (c) 2020 Antony Pavlov <antonynpavlov@gmail.com>
3  *
4  * based on include/arch/sparc/arch.h
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #ifndef ZEPHYR_INCLUDE_ARCH_MIPS_ARCH_H_
10 #define ZEPHYR_INCLUDE_ARCH_MIPS_ARCH_H_
11 
12 #include <zephyr/arch/mips/thread.h>
13 #include <zephyr/arch/mips/exception.h>
14 #include <zephyr/arch/common/sys_bitops.h>
15 #include <zephyr/arch/common/sys_io.h>
16 #include <zephyr/arch/common/ffs.h>
17 
18 #include <zephyr/irq.h>
19 #include <zephyr/sw_isr_table.h>
20 #include <zephyr/devicetree.h>
21 #include <mips/mipsregs.h>
22 
23 #define ARCH_STACK_PTR_ALIGN 16
24 
25 #define OP_LOADREG lw
26 #define OP_STOREREG sw
27 
28 #define CP0_STATUS_DEF_RESTORE (ST0_EXL | ST0_IE)
29 
30 #ifndef _ASMLANGUAGE
31 #include <zephyr/sys/util.h>
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 #define STACK_ROUND_UP(x) ROUND_UP(x, ARCH_STACK_PTR_ALIGN)
38 
39 void arch_irq_enable(unsigned int irq);
40 void arch_irq_disable(unsigned int irq);
41 int arch_irq_is_enabled(unsigned int irq);
42 void z_irq_spurious(const void *unused);
43 
44 /**
45  * Configure a static interrupt.
46  *
47  * All arguments must be computable by the compiler at build time.
48  *
49  * @param irq_p IRQ line number
50  * @param priority_p Interrupt priority
51  * @param isr_p Interrupt service routine
52  * @param isr_param_p ISR parameter
53  * @param flags_p IRQ options
54  *
55  * @return The vector assigned to this interrupt
56  */
57 
58 #define ARCH_IRQ_CONNECT(irq_p, priority_p, isr_p, isr_param_p, flags_p) \
59 	{								 \
60 		Z_ISR_DECLARE(irq_p, 0, isr_p, isr_param_p);		 \
61 	}
62 
arch_irq_lock(void)63 static ALWAYS_INLINE unsigned int arch_irq_lock(void)
64 {
65 	uint32_t status = read_c0_status();
66 
67 	if (status & ST0_IE) {
68 		write_c0_status(status & ~ST0_IE);
69 		return 1;
70 	}
71 	return 0;
72 }
73 
arch_irq_unlock(unsigned int key)74 static ALWAYS_INLINE void arch_irq_unlock(unsigned int key)
75 {
76 	uint32_t status = read_c0_status();
77 
78 	if (key) {
79 		status |= ST0_IE;
80 	} else {
81 		status &= ~ST0_IE;
82 	}
83 
84 	write_c0_status(status);
85 }
86 
arch_irq_unlocked(unsigned int key)87 static ALWAYS_INLINE bool arch_irq_unlocked(unsigned int key)
88 {
89 	return key != 0;
90 }
91 
arch_nop(void)92 static ALWAYS_INLINE void arch_nop(void)
93 {
94 	__asm__ volatile ("nop");
95 }
96 
97 extern uint32_t sys_clock_cycle_get_32(void);
98 
arch_k_cycle_get_32(void)99 static inline uint32_t arch_k_cycle_get_32(void)
100 {
101 	return sys_clock_cycle_get_32();
102 }
103 
104 extern uint64_t sys_clock_cycle_get_64(void);
105 
arch_k_cycle_get_64(void)106 static inline uint64_t arch_k_cycle_get_64(void)
107 {
108 	return sys_clock_cycle_get_64();
109 }
110 
111 #ifdef __cplusplus
112 }
113 #endif
114 
115 #endif /* _ASMLANGUAGE */
116 
117 #endif /* ZEPHYR_INCLUDE_ARCH_MIPS_ARCH_H_ */
118