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