|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Nordic Semiconductor ASA |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +#define DT_DRV_COMPAT nordic_nrf_iron_hsfll_local |
| 7 | + |
| 8 | +#include "clock_control_nrf2_common.h" |
| 9 | +#include <zephyr/devicetree.h> |
| 10 | +#include <zephyr/drivers/clock_control/nrf_clock_control.h> |
| 11 | + |
| 12 | +#include <zephyr/logging/log.h> |
| 13 | +LOG_MODULE_DECLARE(clock_control_nrf2, CONFIG_CLOCK_CONTROL_LOG_LEVEL); |
| 14 | + |
| 15 | +BUILD_ASSERT(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) == 1, "multiple instances not supported"); |
| 16 | + |
| 17 | +#ifdef CONFIG_NRF_IRONSIDE_DVFS_SERVICE |
| 18 | +#include <zephyr/drivers/firmware/nrf_ironside/dvfs.h> |
| 19 | + |
| 20 | +#define HSFLL_FREQ_LOW MHZ(64) |
| 21 | +#define HSFLL_FREQ_MEDLOW MHZ(128) |
| 22 | +#define HSFLL_FREQ_HIGH MHZ(320) |
| 23 | + |
| 24 | +#define IRONSIDE_DVFS_TIMEOUT K_MSEC(CONFIG_CLOCK_CONTROL_NRF_IRON_HSFLL_LOCAL_DVFS_TIMEOUT_MS) |
| 25 | + |
| 26 | +/* Clock options sorted from lowest to highest frequency */ |
| 27 | +static const struct clock_options { |
| 28 | + uint32_t frequency; |
| 29 | + enum ironside_dvfs_oppoint setting; |
| 30 | +} clock_options[] = { |
| 31 | + { |
| 32 | + .frequency = HSFLL_FREQ_LOW, |
| 33 | + .setting = IRONSIDE_DVFS_OPP_LOW, |
| 34 | + }, |
| 35 | + { |
| 36 | + .frequency = HSFLL_FREQ_MEDLOW, |
| 37 | + .setting = IRONSIDE_DVFS_OPP_MEDLOW, |
| 38 | + }, |
| 39 | + { |
| 40 | + .frequency = HSFLL_FREQ_HIGH, |
| 41 | + .setting = IRONSIDE_DVFS_OPP_HIGH, |
| 42 | + }, |
| 43 | +}; |
| 44 | + |
| 45 | +struct hsfll_dev_data { |
| 46 | + STRUCT_CLOCK_CONFIG(hsfll, ARRAY_SIZE(clock_options)) clk_cfg; |
| 47 | + struct k_timer timer; |
| 48 | +}; |
| 49 | + |
| 50 | +static void hsfll_update_timeout_handler(struct k_timer *timer) |
| 51 | +{ |
| 52 | + struct hsfll_dev_data *dev_data = CONTAINER_OF(timer, struct hsfll_dev_data, timer); |
| 53 | + |
| 54 | + clock_config_update_end(&dev_data->clk_cfg, -ETIMEDOUT); |
| 55 | +} |
| 56 | + |
| 57 | +static void hsfll_work_handler(struct k_work *work) |
| 58 | +{ |
| 59 | + struct hsfll_dev_data *dev_data = CONTAINER_OF(work, struct hsfll_dev_data, clk_cfg.work); |
| 60 | + enum ironside_dvfs_oppoint required_setting; |
| 61 | + uint8_t to_activate_idx; |
| 62 | + int rc; |
| 63 | + |
| 64 | + to_activate_idx = clock_config_update_begin(work); |
| 65 | + required_setting = clock_options[to_activate_idx].setting; |
| 66 | + |
| 67 | + k_timer_start(&dev_data->timer, IRONSIDE_DVFS_TIMEOUT, K_NO_WAIT); |
| 68 | + |
| 69 | + /* Request the DVFS service to change the OPP point. */ |
| 70 | + rc = ironside_dvfs_change_oppoint(required_setting); |
| 71 | + k_timer_stop(&dev_data->timer); |
| 72 | + clock_config_update_end(&dev_data->clk_cfg, rc); |
| 73 | +} |
| 74 | + |
| 75 | +static int hsfll_resolve_spec_to_idx(const struct nrf_clock_spec *req_spec) |
| 76 | +{ |
| 77 | + uint32_t req_frequency; |
| 78 | + |
| 79 | + if (req_spec->accuracy || req_spec->precision) { |
| 80 | + LOG_ERR("invalid specification of accuracy or precision"); |
| 81 | + return -EINVAL; |
| 82 | + } |
| 83 | + |
| 84 | + req_frequency = req_spec->frequency == NRF_CLOCK_CONTROL_FREQUENCY_MAX |
| 85 | + ? HSFLL_FREQ_HIGH |
| 86 | + : req_spec->frequency; |
| 87 | + |
| 88 | + for (int i = 0; i < ARRAY_SIZE(clock_options); ++i) { |
| 89 | + if (req_frequency > clock_options[i].frequency) { |
| 90 | + continue; |
| 91 | + } |
| 92 | + |
| 93 | + return i; |
| 94 | + } |
| 95 | + |
| 96 | + LOG_ERR("invalid frequency"); |
| 97 | + return -EINVAL; |
| 98 | +} |
| 99 | + |
| 100 | +static void hsfll_get_spec_by_idx(uint8_t idx, struct nrf_clock_spec *spec) |
| 101 | +{ |
| 102 | + spec->frequency = clock_options[idx].frequency; |
| 103 | + spec->accuracy = 0; |
| 104 | + spec->precision = 0; |
| 105 | +} |
| 106 | + |
| 107 | +static struct onoff_manager *hsfll_get_mgr_by_idx(const struct device *dev, uint8_t idx) |
| 108 | +{ |
| 109 | + struct hsfll_dev_data *dev_data = dev->data; |
| 110 | + |
| 111 | + return &dev_data->clk_cfg.onoff[idx].mgr; |
| 112 | +} |
| 113 | + |
| 114 | +static struct onoff_manager *hsfll_find_mgr_by_spec(const struct device *dev, |
| 115 | + const struct nrf_clock_spec *spec) |
| 116 | +{ |
| 117 | + int idx; |
| 118 | + |
| 119 | + if (!spec) { |
| 120 | + return hsfll_get_mgr_by_idx(dev, 0); |
| 121 | + } |
| 122 | + |
| 123 | + idx = hsfll_resolve_spec_to_idx(spec); |
| 124 | + return idx < 0 ? NULL : hsfll_get_mgr_by_idx(dev, idx); |
| 125 | +} |
| 126 | +#endif /* CONFIG_NRF_IRONSIDE_DVFS_SERVICE */ |
| 127 | + |
| 128 | +static int api_request_hsfll(const struct device *dev, const struct nrf_clock_spec *spec, |
| 129 | + struct onoff_client *cli) |
| 130 | +{ |
| 131 | +#ifdef CONFIG_NRF_IRONSIDE_DVFS_SERVICE |
| 132 | + struct onoff_manager *mgr = hsfll_find_mgr_by_spec(dev, spec); |
| 133 | + |
| 134 | + if (mgr) { |
| 135 | + return clock_config_request(mgr, cli); |
| 136 | + } |
| 137 | + |
| 138 | + return -EINVAL; |
| 139 | +#else |
| 140 | + return -ENOTSUP; |
| 141 | +#endif |
| 142 | +} |
| 143 | + |
| 144 | +static int api_release_hsfll(const struct device *dev, const struct nrf_clock_spec *spec) |
| 145 | +{ |
| 146 | +#ifdef CONFIG_NRF_IRONSIDE_DVFS_SERVICE |
| 147 | + struct onoff_manager *mgr = hsfll_find_mgr_by_spec(dev, spec); |
| 148 | + |
| 149 | + if (mgr) { |
| 150 | + return onoff_release(mgr); |
| 151 | + } |
| 152 | + |
| 153 | + return -EINVAL; |
| 154 | +#else |
| 155 | + return -ENOTSUP; |
| 156 | +#endif |
| 157 | +} |
| 158 | + |
| 159 | +static int api_cancel_or_release_hsfll(const struct device *dev, const struct nrf_clock_spec *spec, |
| 160 | + struct onoff_client *cli) |
| 161 | +{ |
| 162 | +#ifdef CONFIG_NRF_IRONSIDE_DVFS_SERVICE |
| 163 | + struct onoff_manager *mgr = hsfll_find_mgr_by_spec(dev, spec); |
| 164 | + |
| 165 | + if (mgr) { |
| 166 | + return onoff_cancel_or_release(mgr, cli); |
| 167 | + } |
| 168 | + |
| 169 | + return -EINVAL; |
| 170 | +#else |
| 171 | + return -ENOTSUP; |
| 172 | +#endif |
| 173 | +} |
| 174 | + |
| 175 | +static int api_resolve_hsfll(const struct device *dev, const struct nrf_clock_spec *req_spec, |
| 176 | + struct nrf_clock_spec *res_spec) |
| 177 | +{ |
| 178 | +#ifdef CONFIG_NRF_IRONSIDE_DVFS_SERVICE |
| 179 | + int idx; |
| 180 | + |
| 181 | + idx = hsfll_resolve_spec_to_idx(req_spec); |
| 182 | + if (idx < 0) { |
| 183 | + return -EINVAL; |
| 184 | + } |
| 185 | + |
| 186 | + hsfll_get_spec_by_idx(idx, res_spec); |
| 187 | + return 0; |
| 188 | +#else |
| 189 | + return -ENOTSUP; |
| 190 | +#endif |
| 191 | +} |
| 192 | + |
| 193 | +static int hsfll_init(const struct device *dev) |
| 194 | +{ |
| 195 | +#ifdef CONFIG_NRF_IRONSIDE_DVFS_SERVICE |
| 196 | + struct hsfll_dev_data *dev_data = dev->data; |
| 197 | + int rc; |
| 198 | + |
| 199 | + rc = clock_config_init(&dev_data->clk_cfg, ARRAY_SIZE(dev_data->clk_cfg.onoff), |
| 200 | + hsfll_work_handler); |
| 201 | + if (rc < 0) { |
| 202 | + return rc; |
| 203 | + } |
| 204 | + |
| 205 | + k_timer_init(&dev_data->timer, hsfll_update_timeout_handler, NULL); |
| 206 | + |
| 207 | +#endif |
| 208 | + |
| 209 | + return 0; |
| 210 | +} |
| 211 | + |
| 212 | +static DEVICE_API(nrf_clock_control, hsfll_drv_api) = { |
| 213 | + .std_api = { |
| 214 | + .on = api_nosys_on_off, |
| 215 | + .off = api_nosys_on_off, |
| 216 | + }, |
| 217 | + .request = api_request_hsfll, |
| 218 | + .release = api_release_hsfll, |
| 219 | + .cancel_or_release = api_cancel_or_release_hsfll, |
| 220 | + .resolve = api_resolve_hsfll, |
| 221 | +}; |
| 222 | + |
| 223 | +#ifdef CONFIG_NRF_IRONSIDE_DVFS_SERVICE |
| 224 | +static struct hsfll_dev_data hsfll_data; |
| 225 | +#endif |
| 226 | + |
| 227 | +#ifdef CONFIG_CLOCK_CONTROL_NRF_IRON_HSFLL_LOCAL_REQ_LOW_FREQ |
| 228 | +static int dvfs_low_init(void) |
| 229 | +{ |
| 230 | + static const k_timeout_t timeout = IRONSIDE_DVFS_TIMEOUT; |
| 231 | + static const struct device *hsfll_dev = DEVICE_DT_GET(DT_CLOCKS_CTLR(DT_NODELABEL(cpu))); |
| 232 | + static const struct nrf_clock_spec clk_spec = {.frequency = HSFLL_FREQ_LOW}; |
| 233 | + |
| 234 | + return nrf_clock_control_request_sync(hsfll_dev, &clk_spec, timeout); |
| 235 | +} |
| 236 | + |
| 237 | +SYS_INIT(dvfs_low_init, APPLICATION, 0); |
| 238 | +#endif |
| 239 | + |
| 240 | +DEVICE_DT_INST_DEFINE(0, hsfll_init, NULL, |
| 241 | + COND_CODE_1(CONFIG_NRF_IRONSIDE_DVFS_SERVICE, |
| 242 | + (&hsfll_data), |
| 243 | + (NULL)), NULL, PRE_KERNEL_1, |
| 244 | + CONFIG_CLOCK_CONTROL_INIT_PRIORITY, &hsfll_drv_api); |
0 commit comments