Pavona Software APIs
clkmgr_testutils.c
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#include "sw/device/lib/testing/clkmgr_testutils.h"
6
9
10#define MODULE_ID MAKE_MODULE_ID('c', 'm', 't')
11
12static_assert(kDtClkmgrCount == 1,
13 "this code assumes that there is a single clkmgr");
14
15enum {
16 // We do not know the exact number of measurable clocks but
17 // there cannot be more than the total number of clocks.
18 kMaxMeasureClockCount = kDtClockCount,
19};
20
21// `extern` declarations to give the inline functions in the
22// corresponding header a link location.
23
24extern bool clkmgr_testutils_get_trans_clock_status(
25 const dif_clkmgr_t *clkmgr, dif_clkmgr_hintable_clock_t clock);
26
27extern status_t clkmgr_testutils_check_trans_clock_gating(
28 const dif_clkmgr_t *clkmgr, dif_clkmgr_hintable_clock_t clock,
29 bool exp_clock_enabled, uint32_t timeout_usec);
30
31// The thresholds are encoded as
32// - max = count + variability
33// - min = count - variability
34typedef struct expected_count_info {
35 uint32_t count;
36 uint32_t variability;
37} expected_count_info_t;
38
39// Map from the DT clock to the clock measurement index.
40// For DT clocks without measurements, this will be set to
41// an invalid value.
42static size_t dt_clk_to_meas_clk[kDtClockCount];
43
44// The expected counts when jitter is disabled.
45static expected_count_info_t kNoJitterCountInfos[kMaxMeasureClockCount];
46
47// The expected counts when jitter is enabled.
48static expected_count_info_t kJitterCountInfos[kMaxMeasureClockCount];
49
50// Notice the expected variability is set to somewhat less than the added
51// variability of the AON and the measured clock.
52
53// The expected variability as a percentage. The AST guarantees 3% for all
54// clocks, including the AON, so the effective variability is set to 5%.
55static uint32_t kVariabilityPercentage = 5;
56
57// The expected variability when jitter is enabled as a percentage. The AST
58// guarantees 10% for jittery clocks and 3% for the AON clock, so the
59// effective variability is set to 12%.
60static uint32_t kJitterVariabilityPercentage = 12;
61
62// Compute the variability for a given number of cycles, adding an extra cycle
63// for synchronizers.
64static inline uint32_t get_count_variability(uint32_t cycles,
65 uint32_t variability_percentage) {
66 return ((cycles * variability_percentage) + 99) / 100 + 1;
67}
68
69static const char *measure_clock_name(const dif_clkmgr_t *clkmgr,
70 dif_clkmgr_measure_clock_t clock) {
71 dt_clkmgr_t dt;
72 dif_result_t res = dif_clkmgr_get_dt(clkmgr, &dt);
73 if (res != kDifOk) {
74 return "<no dt info>";
75 }
76 if (clock >= dt_clkmgr_measurable_clock_count(dt)) {
77 return "<invalid clock>";
78 }
80 // This is not ideal because it relies on top specific names but it's better
81 // than just indices.
82 switch (info.clock) {
83#ifdef OPENTITAN_CLKMGR_HAS_MEAS_CTRL_MAIN
84 case kDtClockMain:
85 return "main_clk";
86#endif
87#ifdef OPENTITAN_CLKMGR_HAS_MEAS_CTRL_IO
88 case kDtClockIo:
89 return "io_clk";
90#endif
91#ifdef OPENTITAN_CLKMGR_HAS_MEAS_CTRL_IO_DIV2
92 case kDtClockIoDiv2:
93 return "io_div2_clk";
94#endif
95#ifdef OPENTITAN_CLKMGR_HAS_MEAS_CTRL_IO_DIV4
96 case kDtClockIoDiv4:
97 return "io_div4_clk";
98#endif
99#ifdef OPENTITAN_CLKMGR_HAS_MEAS_CTRL_USB
100 case kDtClockUsb:
101 return "usb_clk";
102#endif
103 default:
104 return "<unknown>";
105 }
106}
107
108static uint32_t cast_safely(uint64_t val) {
109 CHECK(val <= UINT32_MAX);
110 return (uint32_t)val;
111}
112
113dif_result_t initialize_expected_counts(const dif_clkmgr_t *clkmgr) {
114 dt_clkmgr_t clkmgr_dt;
115 DIF_RETURN_IF_ERROR(dif_clkmgr_get_dt(clkmgr, &clkmgr_dt));
116 // We assume that measurements are made against the clk_aon_i port.
117 dt_clock_t aon_clk_dt = dt_clkmgr_clock(clkmgr_dt, kDtClkmgrClockAon);
118 dt_clock_t main_clk_dt = dt_clkmgr_clock(clkmgr_dt, kDtClkmgrClockMain);
119 uint64_t aon_clk_freq_hz = dt_clock_frequency(aon_clk_dt);
120
121 // Fill the clock map with invalid values.
122 for (dt_clock_t dt_clk = 0; dt_clk < kDtClockCount; dt_clk++) {
123 dt_clk_to_meas_clk[dt_clk] = dt_clkmgr_measurable_clock_count(clkmgr_dt);
124 }
125
126 // Go through all measurable clocks.
127 for (size_t clk_idx = 0;
128 clk_idx < dt_clkmgr_measurable_clock_count(clkmgr_dt); clk_idx++) {
129 dt_clock_t clk_dt = dt_clkmgr_measurable_clock(clkmgr_dt, clk_idx).clock;
130 dt_clk_to_meas_clk[clk_dt] = clk_idx;
131
132 uint64_t clk_freq_hz = dt_clock_frequency(clk_dt);
133 // The expected counts are derived from the ratios of the frequencies of the
134 // various clocks to the AON clock. For example, 48 Mhz / 200 kHz = 240.
135 // Notice the ratios are small enough to fit a uint32_t, even if the Hz
136 // number is in uint64_t.
137 uint32_t clk_count = cast_safely(
138 udiv64_slow(clk_freq_hz, aon_clk_freq_hz, /*rem_out=*/NULL));
139 // If the clock count is below 100, run for multiple reference clocks
140 // until the clock count is over 100. Notice this matches how the
141 // clock measurement RTL is configured.
142 if (clk_count < 100) {
143 uint32_t original_clk_count = clk_count;
144 for (int i = 1;; i++) {
145 clk_count <<= 1;
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);
149 break;
150 }
151 }
152 }
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,
157 variability);
158
159 // Each clock count is guaranteed by the AST +- 3%. This includes the AON
160 // clock, so we use an effective variability of +- 5%.
161 kNoJitterCountInfos[clk_idx] = (expected_count_info_t){
162 .count = clk_count - 1, .variability = variability};
163
164 // When jitter is enabled only the main clk is affected: the low threshold
165 // should be up to 20% lower, so the expected count is set to 0.9 max, and
166 // the variability is set per kJitterVariabilityPercentage.
167 if (clk_dt != main_clk_dt) {
168 kJitterCountInfos[clk_idx] = kNoJitterCountInfos[clk_idx];
169 } else {
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)};
174 }
175 }
176
177 return kDifOk;
178}
179
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,
186 hi_threshold);
187 TRY(dif_clkmgr_enable_measure_counts(clkmgr, clock, lo_threshold,
188 hi_threshold));
189 return OK_STATUS();
190}
191
192status_t clkmgr_testutils_enable_clock_counts_with_expected_thresholds(
193 const dif_clkmgr_t *clkmgr, bool jitter_enabled, bool external_clk,
194 bool low_speed) {
195 static bool counts_initialized = false;
196 if (!counts_initialized) {
197 TRY(initialize_expected_counts(clkmgr));
198 counts_initialized = true;
199 }
200 TRY_CHECK(!(external_clk && jitter_enabled));
201
202 dt_clkmgr_t clkmgr_dt;
203 TRY(dif_clkmgr_get_dt(clkmgr, &clkmgr_dt));
204
205 for (size_t clk = 0; clk < dt_clkmgr_measurable_clock_count(clkmgr_dt);
206 ++clk) {
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)
212 dt_clock_t dt_actual_clk =
213 dt_clkmgr_measurable_clock(clkmgr_dt, clk).clock;
214 // When software switches to an external clock, all clock sources switch
215 // to an external source. In particular, the main clock becomes the same
216 // as the IO clock.
217 if (dt_actual_clk == kDtClockMain) {
218 dt_actual_clk = kDtClockIo;
219 }
220 // If software requests a low speed external clock, internal dividers are
221 // stepped down so that divide-by-2 and divide-by-4 remain at their
222 // nominal frequencies. On the other hand, the divide-by-1 (i.e. the IO
223 // clock) becomes equal to divide-by-2.
224 if (low_speed && dt_actual_clk == kDtClockIo) {
225 dt_actual_clk = kDtClockIoDiv2;
226 }
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");
234#else
235#error Unsupported top
236#endif
237 } else {
238 count_info = &kNoJitterCountInfos[clk];
239 }
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));
244 }
245 return OK_STATUS();
246}
247
248status_t clkmgr_testutils_enable_clock_counts_with_failing_thresholds(
249 const dif_clkmgr_t *clkmgr) {
250 CHECK_STATUS_OK(clkmgr_testutils_enable_clock_counts_with_expected_thresholds(
251 clkmgr, /*jitter_enabled=*/false, /*external_clk=*/false,
252 /*low_speed=*/true));
253 dt_clkmgr_t clkmgr_dt;
254 CHECK_DIF_OK(dif_clkmgr_get_dt(clkmgr, &clkmgr_dt));
255
256 for (size_t clk = 0; clk < dt_clkmgr_measurable_clock_count(clkmgr_dt);
257 ++clk) {
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));
264 }
265 return OK_STATUS();
266}
267
268status_t clkmgr_testutils_check_measurement_enables(
269 const dif_clkmgr_t *clkmgr, dif_toggle_t expected_status) {
270 bool success = true;
271 dt_clkmgr_t clkmgr_dt;
272 TRY(dif_clkmgr_get_dt(clkmgr, &clkmgr_dt));
273 for (size_t i = 0; i < dt_clkmgr_measurable_clock_count(clkmgr_dt); ++i) {
274 dif_clkmgr_measure_clock_t clock = (dif_clkmgr_measure_clock_t)i;
275 dif_toggle_t actual_status;
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,
279 (expected_status == kDifToggleEnabled ? "enabled" : "disabled"));
280 success = false;
281 }
282 }
283 return OK_STATUS(success);
284}
285
286status_t clkmgr_testutils_disable_clock_counts(const dif_clkmgr_t *clkmgr) {
287 LOG_INFO("Disabling all clock count measurements");
288 dt_clkmgr_t clkmgr_dt;
289 TRY(dif_clkmgr_get_dt(clkmgr, &clkmgr_dt));
290 for (size_t i = 0; i < dt_clkmgr_measurable_clock_count(clkmgr_dt); ++i) {
291 dif_clkmgr_measure_clock_t clock = (dif_clkmgr_measure_clock_t)i;
292 TRY(dif_clkmgr_disable_measure_counts(clkmgr, clock));
293 }
294 LOG_INFO("Disabling all clock count done");
295 return OK_STATUS();
296}
297
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);
304 result = INTERNAL();
305 } else {
306 LOG_INFO("Clock measurements are okay");
307 }
308 // Clear recoverable errors.
309 TRY(dif_clkmgr_recov_err_code_clear_codes(clkmgr, ~0u));
310 return result;
311}
312
313status_t clkmgr_testutils_enable_external_clock_blocking(
314 const dif_clkmgr_t *clkmgr, bool is_low_speed) {
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");
320#endif
321 return OK_STATUS();
322}