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 dif_clkmgr_t clkmgr;
51 static dif_pwrmgr_t pwrmgr;
52 
53 // Test with thresholds for jitter enabled expecting no failures, and then
54 // with thresholds for jitter disabled expecting failures.
55 static void test_clock_frequencies_with_jitter_enabled(uint32_t delay_micros) {
56  LOG_INFO("Testing frequencies with jitter enabled");
57  CHECK_STATUS_OK(clkmgr_testutils_enable_clock_counts_with_expected_thresholds(
58  &clkmgr, /*jitter_enabled=*/true, /*external_clk=*/false,
59  /*low_speed=*/false));
60  busy_spin_micros(delay_micros);
61  // This checks there are no errors.
62  CHECK_STATUS_OK(clkmgr_testutils_check_measurement_counts(&clkmgr));
63  CHECK_STATUS_OK(clkmgr_testutils_disable_clock_counts(&clkmgr));
65  // Set thresholds for jitter disabled expecting failures.
66  CHECK_STATUS_OK(
67  clkmgr_testutils_enable_clock_counts_with_expected_thresholds(
68  &clkmgr, /*jitter_enabled=*/false, /*external_clk=*/false,
69  /*low_speed=*/false));
70  busy_spin_micros(delay_micros);
72  CHECK_DIF_OK(dif_clkmgr_recov_err_code_get_codes(&clkmgr, &err_codes));
73  CHECK(err_codes != 0);
74  // Clear errors.
75  CHECK_STATUS_OK(clkmgr_testutils_disable_clock_counts(&clkmgr));
76  CHECK_DIF_OK(dif_clkmgr_recov_err_code_clear_codes(&clkmgr, err_codes));
77  } else {
78  LOG_INFO("Testing with jitter enabled but no-jitter thresholds %s",
79  "is not viable for FPGAs");
80  }
81 }
82 
83 static void test_clock_frequencies_with_jitter_disabled(uint32_t delay_micros) {
84  LOG_INFO("Testing frequencies with jitter disabled");
85  CHECK_STATUS_OK(clkmgr_testutils_enable_clock_counts_with_expected_thresholds(
86  &clkmgr, /*jitter_enabled=*/false, /*external_clk=*/false,
87  /*low_speed=*/false));
88  busy_spin_micros(delay_micros);
89  // This checks there are no errors.
90  CHECK_STATUS_OK(clkmgr_testutils_check_measurement_counts(&clkmgr));
91  CHECK_STATUS_OK(clkmgr_testutils_disable_clock_counts(&clkmgr));
92  // Set thresholds for jitter disabled expecting no failures.
93  CHECK_STATUS_OK(clkmgr_testutils_enable_clock_counts_with_expected_thresholds(
94  &clkmgr, /*jitter_enabled=*/true, /*external_clk=*/false,
95  /*low_speed=*/false));
96  busy_spin_micros(delay_micros);
97  LOG_INFO("Checking measurement counts");
98  CHECK_STATUS_OK(clkmgr_testutils_check_measurement_counts(&clkmgr));
99 }
100 
101 bool test_main(void) {
102  dif_sensor_ctrl_t sensor_ctrl;
103 
104  uint32_t delay_micros = 0;
105  CHECK_STATUS_OK(aon_timer_testutils_get_us_from_aon_cycles(
106  kMeasurementsPerRound, &delay_micros));
107 
108  CHECK_DIF_OK(dif_clkmgr_init(
110  CHECK_DIF_OK(dif_sensor_ctrl_init(
112  &sensor_ctrl));
113  CHECK_DIF_OK(dif_pwrmgr_init(
115 
116  LOG_INFO("TEST: wait for ast init");
117  IBEX_SPIN_FOR(sensor_ctrl_ast_init_done(&sensor_ctrl), 1000);
118  LOG_INFO("TEST: done ast init");
119 
120  CHECK(UNWRAP(pwrmgr_testutils_is_wakeup_reason(&pwrmgr, 0)) == true);
121 
122  bool jitter_locked;
123  CHECK_DIF_OK(dif_clkmgr_jitter_enable_is_locked(&clkmgr, &jitter_locked));
124  if (jitter_locked) {
125  dif_toggle_t jitter_status;
126  CHECK_DIF_OK(dif_clkmgr_jitter_get_enabled(&clkmgr, &jitter_status));
127  if (jitter_status == kDifToggleEnabled) {
128  test_clock_frequencies_with_jitter_enabled(delay_micros);
129  } else {
130  test_clock_frequencies_with_jitter_disabled(delay_micros);
131  }
132  } else {
133  CHECK_DIF_OK(dif_clkmgr_jitter_set_enabled(&clkmgr, kDifToggleEnabled));
134  test_clock_frequencies_with_jitter_enabled(delay_micros);
135  CHECK_DIF_OK(dif_clkmgr_jitter_set_enabled(&clkmgr, kDifToggleDisabled));
136  test_clock_frequencies_with_jitter_disabled(delay_micros);
137  }
138  return true;
139 }