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  * Obtain a bit mask of wakeups/reset requests for a device.
205  *
206  * Given a module instance (identified by its instance ID) and a wakeup
207  * or reset request index from this module, return a bitmask of sources which
208  * can be used with the pwrmgr DIF or testutils.
209  *
210  * Example (find request source of the aon_timer wakeup):
211  * ```c
212  * dif_pwrmgr_request_sources_t wakeup_sources;
213  * CHECK_DIF_OK(dif_pwrmgr_find_request_source(
214  * pwrmgr,
215  * kDifPwrmgrReqTypeWakeup,
216  * dt_aon_timer_instance_id(kDtAonTimerAon),
217  * kDtAonTimerWakeupWkupReq,
218  * &wakeup_sources));
219  * ```
220  *
221  * @param pwrmgr A power manager handle.
222  * @param req_type Request type (wake up or reset request).
223  * @param inst_id An instance ID.
224  * @param idx Signal index.
225  * @param[out] sources The bitmask corresponding to the wakeup or reset
226  * requested.
227  * @return `kDifError` if no signal matches the description, `kDifOk` otherwise.
228  */
231  const dif_pwrmgr_t *pwrmgr, dif_pwrmgr_req_type_t req_type,
232  dt_instance_id_t inst_id, size_t idx,
234 
235 /**
236  * Enables or disables low power state.
237  *
238  * When enabled, the power manager transitions to low power state on the next
239  * wait for interrupt (WFI) instruction. Since the hardware clears the
240  * corresponding bit automatically, this function must be called before each
241  * transition to low power state.
242  *
243  * This function can be configured to skip synchronization to the slow clock
244  * domain, under the assumption that timely synchronization will be performed
245  * by some of the other functions that can trigger it.
246  *
247  * @param pwrmgr A power manager handle.
248  * @param new_state Whether low power state is enabled.
249  * @param sync_state Whether to wait for state to transfer to slow domain
250  * @return The result of the operation.
251  */
253 dif_result_t dif_pwrmgr_low_power_set_enabled(const dif_pwrmgr_t *pwrmgr,
254  dif_toggle_t new_state,
255  dif_toggle_t sync_state);
256 
257 /**
258  * Checks whether low power state is enabled.
259  *
260  * @param pwrmgr A power manager handle.
261  * @param[out] cur_state Whether low power state is enabled.
262  * @return The result of the operation.
263  */
265 dif_result_t dif_pwrmgr_low_power_get_enabled(const dif_pwrmgr_t *pwrmgr,
266  dif_toggle_t *cur_state);
267 
268 /**
269  * Configures power manager to enable/disable various clock and power domains in
270  * low and active power states.
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 config A domain configuration.
278  * @param sync_state Whether to wait for state to transfer to slow domain
279  * @return The result of the operation.
280  */
282 dif_result_t dif_pwrmgr_set_domain_config(const dif_pwrmgr_t *pwrmgr,
284  dif_toggle_t sync_state);
285 
286 /**
287  * Gets current power manager configuration.
288  *
289  * @param pwrmgr A power manager handle.
290  * @param[out] config Current configuration.
291  * @return The result of the operation.
292  */
294 dif_result_t dif_pwrmgr_get_domain_config(const dif_pwrmgr_t *pwrmgr,
296 
297 /**
298  * Sets sources enabled for a request type.
299  *
300  * A wakeup or reset request can be triggered by multiple sources, e.g. GPIO,
301  * watchdog timer, USB, etc. This function sets which sources are enabled for a
302  * particular request type.
303  *
304  * This function can be configured to skip synchronization to the slow clock
305  * domain, under the assumption that timely synchronization will be performed
306  * by some of the other functions that can trigger it.
307  *
308  * @param pwrmgr A power manager handle.
309  * @param req_type A request type.
310  * @param sources Sources enabled for the given request type.
311  * @param sync_state Whether to wait for state to transfer to slow domain
312  * @return The result of the operation.
313  */
316  const dif_pwrmgr_t *pwrmgr, dif_pwrmgr_req_type_t req_type,
317  dif_pwrmgr_request_sources_t sources, dif_toggle_t sync_state);
318 
319 /**
320  * Gets sources enabled for a request type.
321  *
322  * A wakeup or reset request can be triggered by multiple sources, e.g. GPIO,
323  * watchdog timer, USB, etc. This function gets which sources are enabled for a
324  * particular request type.
325  *
326  * @param pwrmgr A power manager handle.
327  * @param req_type A request type.
328  * @param[out] sources Sources enabled for the given request type.
329  * @return The result of the operation.
330  */
333  const dif_pwrmgr_t *pwrmgr, dif_pwrmgr_req_type_t req_type,
335 
336 /**
337  * Gets request sources that are currently active for a request type.
338  *
339  * @param pwrmgr A power manager handle.
340  * @param req_type A request type.
341  * @param[out] sources Request sources that are currently active for the given
342  * request type.
343  * @return The result of the operation.
344  */
347  const dif_pwrmgr_t *pwrmgr, dif_pwrmgr_req_type_t req_type,
349 
350 /**
351  * Locks sources of a request type.
352  *
353  * Once the sources of a particular request type is locked, they cannot be
354  * changed until the hardware is reset.
355  *
356  * @param pwrmgr A power manager handle.
357  * @param req_type A request type.
358  * @return The result of the operation.
359  */
361 dif_result_t dif_pwrmgr_request_sources_lock(const dif_pwrmgr_t *pwrmgr,
362  dif_pwrmgr_req_type_t req_type);
363 
364 /**
365  * Checks whether sources of a request type is locked.
366  *
367  * @param pwrmgr A power manager handle.
368  * @param req_type A request type.
369  * @param[out] is_locked Whether sources of the given request type is locked.
370  * @return The result of the operation.
371  */
374  const dif_pwrmgr_t *pwrmgr, dif_pwrmgr_req_type_t req_type,
375  bool *is_locked);
376 
377 /**
378  * Enables or disables recording of wakeup requests.
379  *
380  * Power manager automatically starts recording wakeup requests when it
381  * begins a valid low power entry. Recording continues until it is explicitly
382  * disabled by calling this function.
383  *
384  * @param pwrmgr A power manager handle.
385  * @param new_state Whether wakeup requests should be recorded.
386  * @return The result of the operation.
387  */
390  const dif_pwrmgr_t *pwrmgr, dif_toggle_t new_state);
391 
392 /**
393  * Checks whether wakeup requests are being recorded.
394  *
395  * @param pwrmgr A power manager handle.
396  * @param[out] cur_state Whether wakeup requests are being recorded.
397  * @return The result of the operation.
398  */
401  const dif_pwrmgr_t *pwrmgr, dif_toggle_t *cur_state);
402 
403 /**
404  * Gets wakeup reason and source requests since the last time recording
405  * started.
406  *
407  * Power manager automatically starts recording wakeup requests when it
408  * begins a valid low power entry. Recording continues until it is explicitly
409  * disabled by calling `dif_pwrmgr_wakeup_request_recording_set_enabled`. Thus,
410  * it is possible to record wakeup requests from multiple sources as well as
411  * multiple wakeup types.
412  *
413  * @param pwrmgr A power manager handle.
414  * @param[out] reason Wakeup reasons.
415  * @return The result of the operation.
416  */
418 dif_result_t dif_pwrmgr_wakeup_reason_get(const dif_pwrmgr_t *pwrmgr,
420 
421 /**
422  * Clears wakeup reason(s) recorded since the last time recording started.
423  *
424  * @param pwrmgr A power manager handle.
425  * @return The result of the operation.
426  */
428 dif_result_t dif_pwrmgr_wakeup_reason_clear(const dif_pwrmgr_t *pwrmgr);
429 
430 /**
431  * Read the fatal error codes.
432  *
433  * @param pwrmgr Power Manager Handle.
434  * @param[out] codes The fatal error codes.
435  * @returns The result of the operation.
436  */
439  const dif_pwrmgr_t *pwrmgr, dif_pwrmgr_fatal_err_codes_t *codes);
440 
441 #ifdef __cplusplus
442 } // extern "C"
443 #endif // __cplusplus
444 
445 #endif // OPENTITAN_SW_DEVICE_LIB_DIF_DIF_PWRMGR_H_