5#include "sw/device/lib/testing/clkmgr_testutils.h"
10#define MODULE_ID MAKE_MODULE_ID('c', 'm', 't')
13 "this code assumes that there is a single clkmgr");
24extern bool clkmgr_testutils_get_trans_clock_status(
27extern status_t clkmgr_testutils_check_trans_clock_gating(
29 bool exp_clock_enabled, uint32_t timeout_usec);
37} expected_count_info_t;
45static expected_count_info_t kNoJitterCountInfos[kMaxMeasureClockCount];
48static expected_count_info_t kJitterCountInfos[kMaxMeasureClockCount];
55static uint32_t kVariabilityPercentage = 5;
60static uint32_t kJitterVariabilityPercentage = 12;
64static inline uint32_t get_count_variability(uint32_t cycles,
65 uint32_t variability_percentage) {
66 return ((cycles * variability_percentage) + 99) / 100 + 1;
69static const char *measure_clock_name(
const dif_clkmgr_t *clkmgr,
70 dif_clkmgr_measure_clock_t clock) {
74 return "<no dt info>";
77 return "<invalid clock>";
83#ifdef OPENTITAN_CLKMGR_HAS_MEAS_CTRL_MAIN
87#ifdef OPENTITAN_CLKMGR_HAS_MEAS_CTRL_IO
91#ifdef OPENTITAN_CLKMGR_HAS_MEAS_CTRL_IO_DIV2
95#ifdef OPENTITAN_CLKMGR_HAS_MEAS_CTRL_IO_DIV4
99#ifdef OPENTITAN_CLKMGR_HAS_MEAS_CTRL_USB
108static uint32_t cast_safely(uint64_t val) {
109 CHECK(val <= UINT32_MAX);
110 return (uint32_t)val;
127 for (
size_t clk_idx = 0;
130 dt_clk_to_meas_clk[clk_dt] = clk_idx;
137 uint32_t clk_count = cast_safely(
138 udiv64_slow(clk_freq_hz, aon_clk_freq_hz, NULL));
142 if (clk_count < 100) {
143 uint32_t original_clk_count = clk_count;
144 for (
int i = 1;; i++) {
146 if (clk_count > 100) {
147 LOG_INFO(
"The original clock count was %d, and became %d, shift %d",
148 original_clk_count, clk_count, i);
153 uint32_t variability =
154 get_count_variability(clk_count, kVariabilityPercentage);
155 LOG_INFO(
"Variability for %s(%d) %d is %d",
156 measure_clock_name(clkmgr, clk_idx), clk_idx, clk_count,
161 kNoJitterCountInfos[clk_idx] = (expected_count_info_t){
162 .count = clk_count - 1, .variability = variability};
167 if (clk_dt != main_clk_dt) {
168 kJitterCountInfos[clk_idx] = kNoJitterCountInfos[clk_idx];
170 kJitterCountInfos[clk_idx] = (expected_count_info_t){
171 .count = clk_count - clk_count / 10,
172 .variability = get_count_variability(clk_count - clk_count / 10,
173 kJitterVariabilityPercentage)};
180status_t clkmgr_testutils_enable_clock_count(
const dif_clkmgr_t *clkmgr,
181 dif_clkmgr_measure_clock_t clock,
182 uint32_t lo_threshold,
183 uint32_t hi_threshold) {
184 LOG_INFO(
"Enabling clock count measurement for %s(%d) lo %d hi %d",
185 measure_clock_name(clkmgr, clock), clock, lo_threshold,
187 TRY(dif_clkmgr_enable_measure_counts(clkmgr, clock, lo_threshold,
192status_t clkmgr_testutils_enable_clock_counts_with_expected_thresholds(
193 const dif_clkmgr_t *clkmgr,
bool jitter_enabled,
bool external_clk,
195 static bool counts_initialized =
false;
196 if (!counts_initialized) {
197 TRY(initialize_expected_counts(clkmgr));
198 counts_initialized =
true;
200 TRY_CHECK(!(external_clk && jitter_enabled));
203 TRY(dif_clkmgr_get_dt(clkmgr, &clkmgr_dt));
207 const expected_count_info_t *count_info;
208 if (jitter_enabled) {
209 count_info = &kJitterCountInfos[clk];
210 }
else if (external_clk) {
211#if defined(OPENTITAN_IS_EGRET)
224 if (low_speed && dt_actual_clk ==
kDtClockIo) {
227 CHECK(dt_clk_to_meas_clk[dt_actual_clk] <
229 "this clock is not measurable!");
230 count_info = &kNoJitterCountInfos[dt_clk_to_meas_clk[dt_actual_clk]];
231#elif defined(OPENTITAN_IS_DRAGONFLY)
232 TRY_CHECK(
false,
"Dragonfly has no external clock");
235#error Unsupported top
238 count_info = &kNoJitterCountInfos[clk];
240 TRY(clkmgr_testutils_enable_clock_count(
241 clkmgr, (dif_clkmgr_measure_clock_t)clk,
242 count_info->count - count_info->variability,
243 count_info->count + count_info->variability));
248status_t clkmgr_testutils_enable_clock_counts_with_failing_thresholds(
250 CHECK_STATUS_OK(clkmgr_testutils_enable_clock_counts_with_expected_thresholds(
251 clkmgr,
false,
false,
254 CHECK_DIF_OK(dif_clkmgr_get_dt(clkmgr, &clkmgr_dt));
258 uint32_t min_threshold;
259 uint32_t max_threshold;
260 CHECK_DIF_OK(dif_clkmgr_measure_counts_get_thresholds(
261 clkmgr, clk, &min_threshold, &max_threshold));
262 CHECK_STATUS_OK(clkmgr_testutils_enable_clock_count(
263 clkmgr, clk, min_threshold / 2, max_threshold / 2));
268status_t clkmgr_testutils_check_measurement_enables(
272 TRY(dif_clkmgr_get_dt(clkmgr, &clkmgr_dt));
274 dif_clkmgr_measure_clock_t clock = (dif_clkmgr_measure_clock_t)i;
276 TRY(dif_clkmgr_measure_counts_get_enable(clkmgr, clock, &actual_status));
277 if (actual_status != expected_status) {
278 LOG_INFO(
"Unexpected enable for clock %d: expected %s", i,
283 return OK_STATUS(success);
286status_t clkmgr_testutils_disable_clock_counts(
const dif_clkmgr_t *clkmgr) {
287 LOG_INFO(
"Disabling all clock count measurements");
289 TRY(dif_clkmgr_get_dt(clkmgr, &clkmgr_dt));
291 dif_clkmgr_measure_clock_t clock = (dif_clkmgr_measure_clock_t)i;
292 TRY(dif_clkmgr_disable_measure_counts(clkmgr, clock));
294 LOG_INFO(
"Disabling all clock count done");
298status_t clkmgr_testutils_check_measurement_counts(
const dif_clkmgr_t *clkmgr) {
299 status_t result = OK_STATUS();
301 TRY(dif_clkmgr_recov_err_code_get_codes(clkmgr, &err_codes));
302 if (err_codes != 0) {
303 LOG_ERROR(
"Unexpected recoverable error codes 0x%x", err_codes);
306 LOG_INFO(
"Clock measurements are okay");
309 TRY(dif_clkmgr_recov_err_code_clear_codes(clkmgr, ~0u));
313status_t clkmgr_testutils_enable_external_clock_blocking(
315#if defined(OPENTITAN_IS_EGRET)
316 LOG_INFO(
"Configure clkmgr to enable external clock");
317 TRY(dif_clkmgr_external_clock_set_enabled(clkmgr, is_low_speed));
318 TRY(dif_clkmgr_wait_for_ext_clk_switch(clkmgr));
319 LOG_INFO(
"Switching to external clock completes");