Software APIs
dif_pwrmgr.h
Go to the documentation of this file.
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #ifndef OPENTITAN_SW_DEVICE_LIB_DIF_DIF_PWRMGR_H_
6 #define OPENTITAN_SW_DEVICE_LIB_DIF_DIF_PWRMGR_H_
7 
8 /**
9  * @file
10  * @brief <a href="/book/hw/top_earlgrey/ip_autogen/pwrmgr/">Power Manager</a>
11  * Device Interface Functions
12  */
13 
14 #include <stdint.h>
15 
19 
20 #include "sw/device/lib/dif/autogen/dif_pwrmgr_autogen.h"
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif // __cplusplus
25 
26 /**
27  * A request type, i.e. wakeup or reset.
28  */
29 typedef enum dif_pwrmgr_req_type {
30  /**
31  * A wakeup request.
32  */
34  /**
35  * A reset request.
36  */
39 
40 /**
41  * Options for enabling/disabling various clock and power domains
42  * in low and active power states.
43  *
44  * Constants below are bitmasks that can be combined to define configurations.
45  *
46  * See also: `dif_pwrmgr_domain_config_t`.
47  */
49  /**
50  * Enable core clock in low power state.
51  */
53  /**
54  * Enable input/output (IO) clock in low power state.
55  */
57  /**
58  * Enable USB clock in low power state.
59  */
61  /**
62  * Enable USB clock in active power state.
63  */
65  /**
66  * Enable main power domain in low power state.
67  */
70 
71 /**
72  * A set of domain options.
73  *
74  * This type is used for specifying and querying which clock and power domains
75  * are enabled in low and active power states.
76  *
77  * See also: `dif_pwrmgr_domain_option_t`.
78  */
80 
81 /**
82  * A wakeup request source.
83  *
84  * Constants below are bitmasks that can be used to define sets of wakeup
85  * request sources.
86  *
87  * See also: `dif_pwrmgr_request_sources_t`.
88  *
89  * Note: This needs to be updated once the HW is finalized.
90  */
92  kDifPwrmgrWakeupRequestSourceOne = (1u << 0),
93  kDifPwrmgrWakeupRequestSourceTwo = (1u << 1),
94  kDifPwrmgrWakeupRequestSourceThree = (1u << 2),
95  kDifPwrmgrWakeupRequestSourceFour = (1u << 3),
96 #if defined(OPENTITAN_IS_EARLGREY)
97  kDifPwrmgrWakeupRequestSourceFive = (1u << 4),
98  kDifPwrmgrWakeupRequestSourceSix = (1u << 5),
99 #elif defined(OPENTITAN_IS_DARJEELING)
100 // Darjeeling only has four wakeup request sources
101 #else
102 #error "dif_pwrmgr does not support this top"
103 #endif
105 
106 /**
107  * A reset request source.
108  *
109  * Constants below are bitmasks that can be used to define sets of reset
110  * request sources.
111  *
112  * See also: `dif_pwrmgr_request_sources_t`.
113  *
114  * Note: This needs to be updated once the HW is finalized.
115  */
117  kDifPwrmgrResetRequestSourceOne = (1u << 0),
118  kDifPwrmgrResetRequestSourceTwo = (1u << 1),
120 
121 /**
122  * A set of request sources.
123  *
124  * This type is used for specifying which request sources are enabled for a
125  * particular request type, i.e. wakeup or reset, as well querying wakeup
126  * reasons.
127  *
128  * See also: `dif_pwrmgr_wakeup_request_source_t`,
129  * `dif_pwrmgr_reset_request_source_t`.
130  */
132 
133 /**
134  * A wakeup type.
135  *
136  * Constants below are bitmasks that can be used to define sets of wakeup types.
137  *
138  * See also: `dif_pwrmgr_wakeup_types_t`.
139  */
141  /**
142  * Wakeup due to a peripheral request.
143  */
145  /**
146  * Despite low power mode being enabled and executing a wait for interrupt
147  * (WFI) instruction, an interrupt arrived at just the right time to break the
148  * executing core out of WFI.
149  */
151  /**
152  * Despite low power mode being enabled and executing a wait for interrupt
153  * (WFI) instruction, an active flash, life cycle, or OTP operation was
154  * in progress when the power controller attempted to initiate low power
155  * entry.
156  */
159 
160 /**
161  * A set of wakeup types.
162  *
163  * See also: `dif_pwrmgr_wakeup_type_t`.
164  */
166 
168  /**
169  * A fatal error for regfile integrity.
170  */
172  /**
173  * A fatal error for escalation timeout.
174  */
176  /**
177  * A fatal error for main power glitch.
178  */
180 } dif_pwrmgr_fatal_err_type_t;
181 
182 /**
183  * A set of fatal errors.
184  *
185  * This type is used to read the fatal error codes.
186  */
188 
189 /**
190  * Wakeup types and requests from sources since the last time recording started.
191  */
192 typedef struct dif_pwrmgr_wakeup_reason {
193  /**
194  * Wakeup types since the last time recording started.
195  */
197  /**
198  * Sources that requested wakeup since the last time recording started.
199  */
202 
203 /**
204  * Enables or disables low power state.
205  *
206  * When enabled, the power manager transitions to low power state on the next
207  * wait for interrupt (WFI) instruction. Since the hardware clears the
208  * corresponding bit automatically, this function must be called before each
209  * transition to low power state.
210  *
211  * This function can be configured to skip synchronization to the slow clock
212  * domain, under the assumption that timely synchronization will be performed
213  * by some of the other functions that can trigger it.
214  *
215  * @param pwrmgr A power manager handle.
216  * @param new_state Whether low power state is enabled.
217  * @param sync_state Whether to wait for state to transfer to slow domain
218  * @return The result of the operation.
219  */
221 dif_result_t dif_pwrmgr_low_power_set_enabled(const dif_pwrmgr_t *pwrmgr,
222  dif_toggle_t new_state,
223  dif_toggle_t sync_state);
224 
225 /**
226  * Checks whether low power state is enabled.
227  *
228  * @param pwrmgr A power manager handle.
229  * @param[out] cur_state Whether low power state is enabled.
230  * @return The result of the operation.
231  */
233 dif_result_t dif_pwrmgr_low_power_get_enabled(const dif_pwrmgr_t *pwrmgr,
234  dif_toggle_t *cur_state);
235 
236 /**
237  * Configures power manager to enable/disable various clock and power domains in
238  * low and active power states.
239  *
240  * This function can be configured to skip synchronization to the slow clock
241  * domain, under the assumption that timely synchronization will be performed
242  * by some of the other functions that can trigger it.
243  *
244  * @param pwrmgr A power manager handle.
245  * @param config A domain configuration.
246  * @param sync_state Whether to wait for state to transfer to slow domain
247  * @return The result of the operation.
248  */
250 dif_result_t dif_pwrmgr_set_domain_config(const dif_pwrmgr_t *pwrmgr,
252  dif_toggle_t sync_state);
253 
254 /**
255  * Gets current power manager configuration.
256  *
257  * @param pwrmgr A power manager handle.
258  * @param[out] config Current configuration.
259  * @return The result of the operation.
260  */
262 dif_result_t dif_pwrmgr_get_domain_config(const dif_pwrmgr_t *pwrmgr,
264 
265 /**
266  * Sets sources enabled for a request type.
267  *
268  * A wakeup or reset request can be triggered by multiple sources, e.g. GPIO,
269  * watchdog timer, USB, etc. This function sets which sources are enabled for a
270  * particular request type.
271  *
272  * This function can be configured to skip synchronization to the slow clock
273  * domain, under the assumption that timely synchronization will be performed
274  * by some of the other functions that can trigger it.
275  *
276  * @param pwrmgr A power manager handle.
277  * @param req_type A request type.
278  * @param sources Sources enabled for the given request type.
279  * @param sync_state Whether to wait for state to transfer to slow domain
280  * @return The result of the operation.
281  */
284  const dif_pwrmgr_t *pwrmgr, dif_pwrmgr_req_type_t req_type,
285  dif_pwrmgr_request_sources_t sources, dif_toggle_t sync_state);
286 
287 /**
288  * Gets sources enabled for a request type.
289  *
290  * A wakeup or reset request can be triggered by multiple sources, e.g. GPIO,
291  * watchdog timer, USB, etc. This function gets which sources are enabled for a
292  * particular request type.
293  *
294  * @param pwrmgr A power manager handle.
295  * @param req_type A request type.
296  * @param[out] sources Sources enabled for the given request type.
297  * @return The result of the operation.
298  */
301  const dif_pwrmgr_t *pwrmgr, dif_pwrmgr_req_type_t req_type,
303 
304 /**
305  * Gets request sources that are currently active for a request type.
306  *
307  * @param pwrmgr A power manager handle.
308  * @param req_type A request type.
309  * @param[out] sources Request sources that are currently active for the given
310  * request type.
311  * @return The result of the operation.
312  */
315  const dif_pwrmgr_t *pwrmgr, dif_pwrmgr_req_type_t req_type,
317 
318 /**
319  * Locks sources of a request type.
320  *
321  * Once the sources of a particular request type is locked, they cannot be
322  * changed until the hardware is reset.
323  *
324  * @param pwrmgr A power manager handle.
325  * @param req_type A request type.
326  * @return The result of the operation.
327  */
329 dif_result_t dif_pwrmgr_request_sources_lock(const dif_pwrmgr_t *pwrmgr,
330  dif_pwrmgr_req_type_t req_type);
331 
332 /**
333  * Checks whether sources of a request type is locked.
334  *
335  * @param pwrmgr A power manager handle.
336  * @param req_type A request type.
337  * @param[out] is_locked Whether sources of the given request type is locked.
338  * @return The result of the operation.
339  */
342  const dif_pwrmgr_t *pwrmgr, dif_pwrmgr_req_type_t req_type,
343  bool *is_locked);
344 
345 /**
346  * Enables or disables recording of wakeup requests.
347  *
348  * Power manager automatically starts recording wakeup requests when it
349  * begins a valid low power entry. Recording continues until it is explicitly
350  * disabled by calling this function.
351  *
352  * @param pwrmgr A power manager handle.
353  * @param new_state Whether wakeup requests should be recorded.
354  * @return The result of the operation.
355  */
358  const dif_pwrmgr_t *pwrmgr, dif_toggle_t new_state);
359 
360 /**
361  * Checks whether wakeup requests are being recorded.
362  *
363  * @param pwrmgr A power manager handle.
364  * @param[out] cur_state Whether wakeup requests are being recorded.
365  * @return The result of the operation.
366  */
369  const dif_pwrmgr_t *pwrmgr, dif_toggle_t *cur_state);
370 
371 /**
372  * Gets wakeup reason and source requests since the last time recording
373  * started.
374  *
375  * Power manager automatically starts recording wakeup requests when it
376  * begins a valid low power entry. Recording continues until it is explicitly
377  * disabled by calling `dif_pwrmgr_wakeup_request_recording_set_enabled`. Thus,
378  * it is possible to record wakeup requests from multiple sources as well as
379  * multiple wakeup types.
380  *
381  * @param pwrmgr A power manager handle.
382  * @param[out] reason Wakeup reasons.
383  * @return The result of the operation.
384  */
386 dif_result_t dif_pwrmgr_wakeup_reason_get(const dif_pwrmgr_t *pwrmgr,
388 
389 /**
390  * Clears wakeup reason(s) recorded since the last time recording started.
391  *
392  * @param pwrmgr A power manager handle.
393  * @return The result of the operation.
394  */
396 dif_result_t dif_pwrmgr_wakeup_reason_clear(const dif_pwrmgr_t *pwrmgr);
397 
398 /**
399  * Read the fatal error codes.
400  *
401  * @param pwrmgr Power Manager Handle.
402  * @param[out] codes The fatal error codes.
403  * @returns The result of the operation.
404  */
407  const dif_pwrmgr_t *pwrmgr, dif_pwrmgr_fatal_err_codes_t *codes);
408 
409 #ifdef __cplusplus
410 } // extern "C"
411 #endif // __cplusplus
412 
413 #endif // OPENTITAN_SW_DEVICE_LIB_DIF_DIF_PWRMGR_H_