1 /*
2  * Copyright (c) 2018 Foundries.io
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 
9 #include <zephyr/device.h>
10 #include <zephyr/init.h>
11 #include <fsl_clock.h>
12 #include <zephyr/sys/util.h>
13 
14 #if defined(CONFIG_MULTI_LEVEL_INTERRUPTS)
15 #include <errno.h>
16 #include <zephyr/irq_nextlevel.h>
17 #endif
18 
19 #include <soc.h>
20 
21 #define LOG_LEVEL CONFIG_SOC_LOG_LEVEL
22 #include <zephyr/logging/log.h>
23 LOG_MODULE_REGISTER(soc);
24 
25 #define SCG_LPFLL_DISABLE 0U
26 
27 static const struct device *dev_intmux;
28 
29 /*
30  * Run-mode configuration for the fast internal reference clock (FIRC).
31  */
32 static const scg_firc_config_t rv32m1_firc_config = {
33 	.enableMode = kSCG_FircEnable,
34 	.div1 = kSCG_AsyncClkDivBy1,
35 	.div2 = kSCG_AsyncClkDivBy1,
36 	.div3 = kSCG_AsyncClkDivBy1,
37 	.range = kSCG_FircRange48M,
38 	.trimConfig = NULL,
39 };
40 
41 /*
42  * FIRC-based system clock configuration.
43  */
44 static const scg_sys_clk_config_t rv32m1_sys_clk_config_firc = {
45 	.divSlow = kSCG_SysClkDivBy2,
46 	.divBus = kSCG_SysClkDivBy1,
47 	.divExt = kSCG_SysClkDivBy1,
48 	.divCore = kSCG_SysClkDivBy1,
49 	.src = kSCG_SysClkSrcFirc,
50 };
51 
52 /*
53  * LPFLL configuration.
54  */
55 static const scg_lpfll_config_t rv32m1_lpfll_cfg = {
56 	.enableMode = SCG_LPFLL_DISABLE,
57 	.div1 = kSCG_AsyncClkDivBy1,
58 	.div2 = kSCG_AsyncClkDisable,
59 	.div3 = kSCG_AsyncClkDisable,
60 	.range = kSCG_LpFllRange48M,
61 	.trimConfig = NULL,
62 };
63 
sys_arch_reboot(int type)64 void sys_arch_reboot(int type)
65 {
66 	ARG_UNUSED(type);
67 
68 	EVENT_UNIT->SLPCTRL |= EVENT_SLPCTRL_SYSRSTREQST_MASK;
69 }
70 
arch_irq_enable(unsigned int irq)71 void arch_irq_enable(unsigned int irq)
72 {
73 	if (IS_ENABLED(CONFIG_MULTI_LEVEL_INTERRUPTS)) {
74 		unsigned int level = rv32m1_irq_level(irq);
75 
76 		if (level == 1U) {
77 			EVENT_UNIT->INTPTEN |= BIT(rv32m1_level1_irq(irq));
78 			/* Ensures write has finished: */
79 			(void)(EVENT_UNIT->INTPTEN);
80 		} else {
81 			irq_enable_next_level(dev_intmux, irq);
82 		}
83 	} else {
84 		EVENT_UNIT->INTPTEN |= BIT(rv32m1_level1_irq(irq));
85 		(void)(EVENT_UNIT->INTPTEN);
86 	}
87 }
88 
arch_irq_disable(unsigned int irq)89 void arch_irq_disable(unsigned int irq)
90 {
91 	if (IS_ENABLED(CONFIG_MULTI_LEVEL_INTERRUPTS)) {
92 		unsigned int level = rv32m1_irq_level(irq);
93 
94 		if (level == 1U) {
95 			EVENT_UNIT->INTPTEN &= ~BIT(rv32m1_level1_irq(irq));
96 			/* Ensures write has finished: */
97 			(void)(EVENT_UNIT->INTPTEN);
98 		} else {
99 			irq_disable_next_level(dev_intmux, irq);
100 		}
101 	} else {
102 		EVENT_UNIT->INTPTEN &= ~BIT(rv32m1_level1_irq(irq));
103 		(void)(EVENT_UNIT->INTPTEN);
104 	}
105 }
106 
arch_irq_is_enabled(unsigned int irq)107 int arch_irq_is_enabled(unsigned int irq)
108 {
109 	if (IS_ENABLED(CONFIG_MULTI_LEVEL_INTERRUPTS)) {
110 		unsigned int level = rv32m1_irq_level(irq);
111 
112 		if (level == 1U) {
113 			return (EVENT_UNIT->INTPTEN &
114 				BIT(rv32m1_level1_irq(irq))) != 0;
115 		} else {
116 			uint32_t channel, line, ier;
117 
118 			/*
119 			 * Here we break the abstraction and look
120 			 * directly at the INTMUX registers. We can't
121 			 * use the irq_nextlevel.h API, as that only
122 			 * tells us whether some IRQ at the next level
123 			 * is enabled or not.
124 			 */
125 			channel = rv32m1_intmux_channel(irq);
126 			line = rv32m1_intmux_line(irq);
127 			ier = INTMUX->CHANNEL[channel].CHn_IER_31_0 & BIT(line);
128 
129 			return ier != 0U;
130 		}
131 	} else {
132 		return (EVENT_UNIT->INTPTEN & BIT(rv32m1_level1_irq(irq))) != 0;
133 	}
134 }
135 
136 /*
137  * SoC-level interrupt initialization. Clear any pending interrupts or
138  * events, and find the INTMUX device if necessary.
139  *
140  * This gets called as almost the first thing z_cstart() does, so it
141  * will happen before any calls to the _arch_irq_xxx() routines above.
142  */
soc_interrupt_init(void)143 void soc_interrupt_init(void)
144 {
145 	EVENT_UNIT->INTPTPENDCLEAR = 0xFFFFFFFF;
146 	(void)(EVENT_UNIT->INTPTPENDCLEAR); /* Ensures write has finished. */
147 	EVENT_UNIT->EVTPENDCLEAR   = 0xFFFFFFFF;
148 	(void)(EVENT_UNIT->EVTPENDCLEAR); /* Ensures write has finished. */
149 
150 	if (IS_ENABLED(CONFIG_MULTI_LEVEL_INTERRUPTS)) {
151 		dev_intmux = DEVICE_DT_GET(DT_INST(0, openisa_rv32m1_intmux));
152 	}
153 }
154 
155 /**
156  * @brief Switch system clock configuration in run mode.
157  *
158  * Blocks until the updated configuration takes effect.
159  *
160  * @param cfg New system clock configuration
161  */
rv32m1_switch_sys_clk(const scg_sys_clk_config_t * cfg)162 static void rv32m1_switch_sys_clk(const scg_sys_clk_config_t *cfg)
163 {
164 	scg_sys_clk_config_t cur_cfg;
165 
166 	CLOCK_SetRunModeSysClkConfig(cfg);
167 	do {
168 		CLOCK_GetCurSysClkConfig(&cur_cfg);
169 	} while (cur_cfg.src != cfg->src);
170 }
171 
172 /**
173  * @brief Initializes SIRC and switches system clock source to SIRC.
174  */
rv32m1_switch_to_sirc(void)175 static void rv32m1_switch_to_sirc(void)
176 {
177 	const scg_sirc_config_t sirc_config = {
178 		.enableMode = kSCG_SircEnable,
179 		.div1 = kSCG_AsyncClkDisable,
180 		.div2 = kSCG_AsyncClkDivBy2,
181 		.range = kSCG_SircRangeHigh,
182 	};
183 	const scg_sys_clk_config_t sys_clk_config_sirc = {
184 		.divSlow = kSCG_SysClkDivBy4,
185 		.divCore = kSCG_SysClkDivBy1,
186 		.src = kSCG_SysClkSrcSirc,
187 	};
188 
189 	CLOCK_InitSirc(&sirc_config);
190 	rv32m1_switch_sys_clk(&sys_clk_config_sirc);
191 }
192 
193 /**
194  * @brief Setup peripheral clocks
195  *
196  * Setup the peripheral clock sources.
197  */
rv32m1_setup_peripheral_clocks(void)198 static void rv32m1_setup_peripheral_clocks(void)
199 {
200 #if DT_NODE_HAS_STATUS_OKAY(DT_NODELABEL(tpm0))
201 	CLOCK_SetIpSrc(kCLOCK_Tpm0, kCLOCK_IpSrcFircAsync);
202 #endif
203 #if DT_NODE_HAS_STATUS_OKAY(DT_NODELABEL(tpm1))
204 	CLOCK_SetIpSrc(kCLOCK_Tpm1, kCLOCK_IpSrcFircAsync);
205 #endif
206 #if DT_NODE_HAS_STATUS_OKAY(DT_NODELABEL(tpm2))
207 	CLOCK_SetIpSrc(kCLOCK_Tpm2, kCLOCK_IpSrcFircAsync);
208 #endif
209 #if DT_NODE_HAS_STATUS_OKAY(DT_NODELABEL(tpm3))
210 	CLOCK_SetIpSrc(kCLOCK_Tpm3, kCLOCK_IpSrcFircAsync);
211 #endif
212 }
213 
214 /**
215  * @brief Perform basic hardware initialization
216  *
217  * Initializes the base clocks and LPFLL using helpers provided by the HAL.
218  *
219  * @return 0
220  */
soc_early_init_hook(void)221 void soc_early_init_hook(void)
222 {
223 	unsigned int key;
224 
225 
226 	key = irq_lock();
227 
228 	/* Switch to SIRC so we can initialize the FIRC. */
229 	rv32m1_switch_to_sirc();
230 
231 	/* Now that we're running off of SIRC, set up and switch to FIRC. */
232 	CLOCK_InitFirc(&rv32m1_firc_config);
233 	rv32m1_switch_sys_clk(&rv32m1_sys_clk_config_firc);
234 
235 	/* Initialize LPFLL */
236 	CLOCK_InitLpFll(&rv32m1_lpfll_cfg);
237 
238 	/* Initialize peripheral clocks */
239 	rv32m1_setup_peripheral_clocks();
240 
241 	irq_unlock(key);
242 }
243