Software APIs
clkmgr_jitter_frequency_test.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 
9 #include "sw/device/lib/testing/aon_timer_testutils.h"
10 #include "sw/device/lib/testing/clkmgr_testutils.h"
11 #include "sw/device/lib/testing/pwrmgr_testutils.h"
12 #include "sw/device/lib/testing/sensor_ctrl_testutils.h"
13 #include "sw/device/lib/testing/test_framework/check.h"
15 
17 
18 OTTF_DEFINE_TEST_CONFIG();
19 
20 /**
21  * This test measures clock counts with clkmgr frequency measurements and
22  * jitter enabled, performing 100 measurements per round, where a round is
23  * one AON clock cycle. Measurement errors (fast or slow clocks) are recorded
24  * as recoverable errors in clkmgr.
25  *
26  * This assumes clocks have been calibrated:
27  * - for silicon validation this means clocks are calibrated, which means SV1
28  * tasks are completed
29  * - for simulation it requires overriding the hardware behavior via plusargs
30  * so it runs with calibrated USB clock, or the USB clock frequency will be
31  * incorrect.
32  *
33  * When jitter is enabled it checks that using jitter thresholds the checks
34  * pass, and with normal thresholds we encounter recoverable errors.
35  *
36  * When jitter is disabled it checks that neither set of thresholds cause
37  * errors.
38  *
39  * The test flow depends on jitter enable lock:
40  * - if it is locked this only tests for the given jitter configuration.
41  * - if it is unlocked this tests for both jitter enabled and disabled.
42  *
43  * FPGA emulation platforms don't support jittery clocks so some of the
44  * checks are bypassed for them.
45  */
46 enum {
47  kMeasurementsPerRound = 100,
48 };
49 
50 static const dt_pwrmgr_t kPwrmgrDt = 0;
51 static_assert(kDtPwrmgrCount == 1, "this test expects a pwrmgr");
52 static const dt_clkmgr_t kClkmgrDt = 0;
53 static_assert(kDtClkmgrCount == 1, "this test expects a clkmgr");
54 
55 static dif_clkmgr_t clkmgr;
56 static dif_pwrmgr_t pwrmgr;
57 
58 // Test with thresholds for jitter enabled expecting no failures, and then
59 // with thresholds for jitter disabled expecting failures.
60 static void test_clock_frequencies_with_jitter_enabled(uint32_t delay_micros) {
61  LOG_INFO("Testing frequencies with jitter enabled");
62  CHECK_STATUS_OK(clkmgr_testutils_enable_clock_counts_with_expected_thresholds(
63  &clkmgr, /*jitter_enabled=*/true, /*external_clk=*/false,
64  /*low_speed=*/false));
65  busy_spin_micros(delay_micros);
66  // This checks there are no errors.
67  CHECK_STATUS_OK(clkmgr_testutils_check_measurement_counts(&clkmgr));
68  CHECK_STATUS_OK(clkmgr_testutils_disable_clock_counts(&clkmgr));
70  // Set thresholds for jitter disabled expecting failures.
71  CHECK_STATUS_OK(
72  clkmgr_testutils_enable_clock_counts_with_expected_thresholds(
73  &clkmgr, /*jitter_enabled=*/false, /*external_clk=*/false,
74  /*low_speed=*/false));
75  busy_spin_micros(delay_micros);
77  CHECK_DIF_OK(dif_clkmgr_recov_err_code_get_codes(&clkmgr, &err_codes));
78  CHECK(err_codes != 0);
79  // Clear errors.
80  CHECK_STATUS_OK(clkmgr_testutils_disable_clock_counts(&clkmgr));
81  CHECK_DIF_OK(dif_clkmgr_recov_err_code_clear_codes(&clkmgr, err_codes));
82  } else {
83  LOG_INFO("Testing with jitter enabled but no-jitter thresholds %s",
84  "is not viable for FPGAs");
85  }
86 }
87 
88 static void test_clock_frequencies_with_jitter_disabled(uint32_t delay_micros) {
89  LOG_INFO("Testing frequencies with jitter disabled");
90  CHECK_STATUS_OK(clkmgr_testutils_enable_clock_counts_with_expected_thresholds(
91  &clkmgr, /*jitter_enabled=*/false, /*external_clk=*/false,
92  /*low_speed=*/false));
93  busy_spin_micros(delay_micros);
94  // This checks there are no errors.
95  CHECK_STATUS_OK(clkmgr_testutils_check_measurement_counts(&clkmgr));
96  CHECK_STATUS_OK(clkmgr_testutils_disable_clock_counts(&clkmgr));
97  // Set thresholds for jitter disabled expecting no failures.
98  CHECK_STATUS_OK(clkmgr_testutils_enable_clock_counts_with_expected_thresholds(
99  &clkmgr, /*jitter_enabled=*/true, /*external_clk=*/false,
100  /*low_speed=*/false));
101  busy_spin_micros(delay_micros);
102  LOG_INFO("Checking measurement counts");
103  CHECK_STATUS_OK(clkmgr_testutils_check_measurement_counts(&clkmgr));
104 }
105 
106 bool test_main(void) {
107  dif_sensor_ctrl_t sensor_ctrl;
108 
109  uint32_t delay_micros = 0;
110  CHECK_STATUS_OK(aon_timer_testutils_get_us_from_aon_cycles(
111  kMeasurementsPerRound, &delay_micros));
112 
113  CHECK_DIF_OK(dif_clkmgr_init_from_dt(kClkmgrDt, &clkmgr));
114  CHECK_DIF_OK(dif_sensor_ctrl_init(
116  &sensor_ctrl));
117  CHECK_DIF_OK(dif_pwrmgr_init_from_dt(kPwrmgrDt, &pwrmgr));
118 
119  LOG_INFO("TEST: wait for ast init");
120  IBEX_SPIN_FOR(sensor_ctrl_ast_init_done(&sensor_ctrl), 1000);
121  LOG_INFO("TEST: done ast init");
122 
123  CHECK(UNWRAP(pwrmgr_testutils_is_wakeup_reason(&pwrmgr, 0)) == true);
124 
125  bool jitter_locked;
126  CHECK_DIF_OK(dif_clkmgr_jitter_enable_is_locked(&clkmgr, &jitter_locked));
127  if (jitter_locked) {
128  dif_toggle_t jitter_status;
129  CHECK_DIF_OK(dif_clkmgr_jitter_get_enabled(&clkmgr, &jitter_status));
130  if (jitter_status == kDifToggleEnabled) {
131  test_clock_frequencies_with_jitter_enabled(delay_micros);
132  } else {
133  test_clock_frequencies_with_jitter_disabled(delay_micros);
134  }
135  } else {
136  CHECK_DIF_OK(dif_clkmgr_jitter_set_enabled(&clkmgr, kDifToggleEnabled));
137  test_clock_frequencies_with_jitter_enabled(delay_micros);
138  CHECK_DIF_OK(dif_clkmgr_jitter_set_enabled(&clkmgr, kDifToggleDisabled));
139  test_clock_frequencies_with_jitter_disabled(delay_micros);
140  }
141  return true;
142 }