|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Nordic Semiconductor ASA |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +#include <hal/nrf_hsfll.h> |
| 6 | +#include <zephyr/kernel.h> |
| 7 | + |
| 8 | +#include <zephyr/drivers/firmware/nrf_ironside/ironside_dvfs.h> |
| 9 | +#include <zephyr/drivers/firmware/nrf_ironside/call.h> |
| 10 | + |
| 11 | +static enum ironside_dvfs_oppoint current_dvfs_oppoint = IRONSIDE_DVFS_OPP_HIGH; |
| 12 | + |
| 13 | +struct dvfs_hsfll_data_t { |
| 14 | + uint32_t new_f_mult; |
| 15 | + uint32_t new_f_trim_entry; |
| 16 | + uint32_t max_hsfll_freq; |
| 17 | +}; |
| 18 | + |
| 19 | +static const struct dvfs_hsfll_data_t dvfs_hsfll_data[] = { |
| 20 | + /* ABB oppoint 0.8V */ |
| 21 | + { |
| 22 | + .new_f_mult = 20, |
| 23 | + .new_f_trim_entry = 0, |
| 24 | + .max_hsfll_freq = 320000000, |
| 25 | + }, |
| 26 | + /* ABB oppoint 0.6V */ |
| 27 | + { |
| 28 | + .new_f_mult = 8, |
| 29 | + .new_f_trim_entry = 2, |
| 30 | + .max_hsfll_freq = 128000000, |
| 31 | + }, |
| 32 | + /* ABB oppoint 0.5V */ |
| 33 | + { |
| 34 | + .new_f_mult = 4, |
| 35 | + .new_f_trim_entry = 3, |
| 36 | + .max_hsfll_freq = 64000000, |
| 37 | + }, |
| 38 | +}; |
| 39 | + |
| 40 | +BUILD_ASSERT(ARRAY_SIZE(dvfs_hsfll_data) == (IRONSIDE_DVFS_OPPOINT_COUNT), |
| 41 | + "dvfs_hsfll_data size must match number of DVFS oppoints"); |
| 42 | + |
| 43 | +/** |
| 44 | + * @brief Check if the requested oppoint is allowed. |
| 45 | + * |
| 46 | + * @param freq_setting The requested oppoint to check. |
| 47 | + * @return true if the oppoint is allowed, false otherwise. |
| 48 | + */ |
| 49 | +static bool ironside_dvfs_opp_allowed(enum ironside_dvfs_oppoint freq_setting) |
| 50 | +{ |
| 51 | + if (freq_setting == IRONSIDE_DVFS_OPP_HIGH || freq_setting == IRONSIDE_DVFS_OPP_MEDLOW || |
| 52 | + freq_setting == IRONSIDE_DVFS_OPP_LOW) { |
| 53 | + return true; |
| 54 | + } |
| 55 | + |
| 56 | + return false; |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * @brief Check if the requested oppoint change operation is downscaling. |
| 61 | + * |
| 62 | + * @param target_freq_setting The target oppoint to check. |
| 63 | + * @return true if the current oppoint is higher than the target, false otherwise. |
| 64 | + */ |
| 65 | +static bool ironside_dvfs_is_downscaling(enum ironside_dvfs_oppoint target_freq_setting) |
| 66 | +{ |
| 67 | + return current_dvfs_oppoint < target_freq_setting; |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * @brief Configure hsfll depending on selected oppoint |
| 72 | + * |
| 73 | + * @param enum oppoint target operation point |
| 74 | + * @return 0 value indicates no error. |
| 75 | + * @return -EINVAL invalid oppoint or domain. |
| 76 | + */ |
| 77 | +static int32_t ironside_dvfs_configure_hsfll(enum ironside_dvfs_oppoint oppoint) |
| 78 | +{ |
| 79 | + nrf_hsfll_trim_t hsfll_trim = {}; |
| 80 | + uint8_t freq_trim_idx = dvfs_hsfll_data[oppoint].new_f_trim_entry; |
| 81 | + |
| 82 | +#if defined(NRF_APPLICATION) |
| 83 | + hsfll_trim.vsup = NRF_FICR->TRIM.APPLICATION.HSFLL.TRIM.VSUP; |
| 84 | + hsfll_trim.coarse = NRF_FICR->TRIM.APPLICATION.HSFLL.TRIM.COARSE[freq_trim_idx]; |
| 85 | + hsfll_trim.fine = NRF_FICR->TRIM.APPLICATION.HSFLL.TRIM.FINE[freq_trim_idx]; |
| 86 | +#if NRF_HSFLL_HAS_TCOEF_TRIM |
| 87 | + hsfll_trim.tcoef = NRF_FICR->TRIM.APPLICATION.HSFLL.TRIM.TCOEF; |
| 88 | +#endif |
| 89 | +#else |
| 90 | + #error "Only application core is supported for DVFS" |
| 91 | +#endif |
| 92 | + |
| 93 | + nrf_hsfll_clkctrl_mult_set(NRF_HSFLL, dvfs_hsfll_data[oppoint].new_f_mult); |
| 94 | + nrf_hsfll_trim_set(NRF_HSFLL, &hsfll_trim); |
| 95 | + nrf_barrier_w(); |
| 96 | + |
| 97 | + nrf_hsfll_task_trigger(NRF_HSFLL, NRF_HSFLL_TASK_FREQ_CHANGE); |
| 98 | + /* Trigger hsfll task one more time, SEE PAC-4078 */ |
| 99 | + nrf_hsfll_task_trigger(NRF_HSFLL, NRF_HSFLL_TASK_FREQ_CHANGE); |
| 100 | + |
| 101 | + return 0; |
| 102 | +} |
| 103 | + |
| 104 | +/* Function handling steps for DVFS oppoint change. */ |
| 105 | +static int ironside_dvfs_prepare_to_scale(enum ironside_dvfs_oppoint freq_setting) |
| 106 | +{ |
| 107 | + int err = 0; |
| 108 | + |
| 109 | + if (ironside_dvfs_is_downscaling(freq_setting)) { |
| 110 | + err = ironside_dvfs_configure_hsfll(freq_setting); |
| 111 | + } |
| 112 | + |
| 113 | + return err; |
| 114 | +} |
| 115 | + |
| 116 | +/* Update MDK variable which is used by nrfx_coredep_delay_us (k_busy_wait). */ |
| 117 | +static void ironside_dvfs_update_core_clock(enum ironside_dvfs_oppoint oppoint_freq) |
| 118 | +{ |
| 119 | + extern uint32_t SystemCoreClock; |
| 120 | + |
| 121 | + SystemCoreClock = dvfs_hsfll_data[oppoint_freq].max_hsfll_freq; |
| 122 | +} |
| 123 | + |
| 124 | +/* Perform scaling finnish procedure. */ |
| 125 | +static int ironside_dvfs_change_oppoint_complete(enum ironside_dvfs_oppoint dvfs_oppoint) |
| 126 | +{ |
| 127 | + int err = 0; |
| 128 | + |
| 129 | + if (!ironside_dvfs_is_downscaling(dvfs_oppoint)) { |
| 130 | + err = ironside_dvfs_configure_hsfll(dvfs_oppoint); |
| 131 | + } |
| 132 | + |
| 133 | + current_dvfs_oppoint = dvfs_oppoint; |
| 134 | + ironside_dvfs_update_core_clock(dvfs_oppoint); |
| 135 | + |
| 136 | + return err; |
| 137 | +} |
| 138 | + |
| 139 | +/** |
| 140 | + * @brief Check if ABB analog part is locked. |
| 141 | + * |
| 142 | + * @param abb Pointer to ABB peripheral. |
| 143 | + * |
| 144 | + * @return true if ABB is locked, false otherwise. |
| 145 | + */ |
| 146 | +static inline bool sdfw_dvfs_is_abb_locked(NRF_ABB_Type *abb) |
| 147 | +{ |
| 148 | + /* Check if ABB analog part is locked. */ |
| 149 | + return ((abb->STATUSANA & ABB_STATUSANA_LOCKED_Msk) != 0); |
| 150 | +} |
| 151 | + |
| 152 | +/** |
| 153 | + * @brief Request DVFS oppoint change from IRONside secure domain. |
| 154 | + * This function will send a request over IPC to the IRONside secure domain |
| 155 | + * This function is synchronous and will return when the request is completed. |
| 156 | + * |
| 157 | + * @param oppoint @ref enum ironside_dvfs_oppoint |
| 158 | + * @return int |
| 159 | + */ |
| 160 | +static int ironside_dvfs_req_oppoint(enum ironside_dvfs_oppoint oppoint) |
| 161 | +{ |
| 162 | + int err; |
| 163 | + |
| 164 | + struct ironside_call_buf *const buf = ironside_call_alloc(); |
| 165 | + |
| 166 | + buf->id = IRONSIDE_CALL_ID_DVFS_SERVICE_V0; |
| 167 | + buf->args[IRONSIDE_DVFS_SERVICE_OPPOINT_IDX] = oppoint; |
| 168 | + |
| 169 | + ironside_call_dispatch(buf); |
| 170 | + |
| 171 | + if (buf->status == IRONSIDE_CALL_STATUS_RSP_SUCCESS) { |
| 172 | + err = buf->args[IRONSIDE_DVFS_SERVICE_RETCODE_IDX]; |
| 173 | + } else { |
| 174 | + err = buf->status; |
| 175 | + } |
| 176 | + |
| 177 | + ironside_call_release(buf); |
| 178 | + |
| 179 | + return err; |
| 180 | +} |
| 181 | + |
| 182 | +int ironside_dvfs_change_oppoint(enum ironside_dvfs_oppoint dvfs_oppoint) |
| 183 | +{ |
| 184 | + int status = 0; |
| 185 | + |
| 186 | + if (!ironside_dvfs_opp_allowed(dvfs_oppoint)) { |
| 187 | + return -IRONSIDE_DVFS_ERROR_WRONG_OPPOINT; |
| 188 | + } |
| 189 | + |
| 190 | + if (!sdfw_dvfs_is_abb_locked(NRF_ABB)) { |
| 191 | + return -IRONSIDE_DVFS_ERROR_BUSY; |
| 192 | + } |
| 193 | + |
| 194 | + if (dvfs_oppoint == current_dvfs_oppoint) { |
| 195 | + return status; |
| 196 | + } |
| 197 | + |
| 198 | + if (k_is_in_isr()) { |
| 199 | + return -IRONSIDE_DVFS_ERROR_ISR_NOT_ALLOWED; |
| 200 | + } |
| 201 | + |
| 202 | + ironside_dvfs_prepare_to_scale(dvfs_oppoint); |
| 203 | + |
| 204 | + status = ironside_dvfs_req_oppoint(dvfs_oppoint); |
| 205 | + |
| 206 | + if (status != 0) { |
| 207 | + return status; |
| 208 | + } |
| 209 | + status = ironside_dvfs_change_oppoint_complete(dvfs_oppoint); |
| 210 | + |
| 211 | + return status; |
| 212 | +} |
0 commit comments