Software APIs
clkmgr_reset_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 
8 #include "sw/device/lib/testing/aon_timer_testutils.h"
9 #include "sw/device/lib/testing/clkmgr_testutils.h"
10 #include "sw/device/lib/testing/rstmgr_testutils.h"
11 #include "sw/device/lib/testing/sensor_ctrl_testutils.h"
12 #include "sw/device/lib/testing/test_framework/check.h"
14 
16 
17 OTTF_DEFINE_TEST_CONFIG();
18 
19 /**
20  * This test measure clock counts with clkmgr frequency measurements, performing
21  * 100 measurements per round. Measurement errors (fast or slow clocks) are
22  * recorded as recoverable error in clkmgr.
23  *
24  * This test configures the clock thresholds so it generates errors, after 100
25  * measurements it checks that some errors are found, then a reset is triggered,
26  * and it checks the measurements should be disabled, and no errors should be
27  * reported.
28  */
29 enum {
30  kWaitForCSRPolling = 1, // 1us
31  kMeasurementsPerRound = 100,
32 };
33 
34 bool test_main(void) {
35  dif_clkmgr_t clkmgr;
36  dif_rstmgr_t rstmgr;
37  dif_sensor_ctrl_t sensor_ctrl;
38 
39  uint32_t delay_micros = 0;
40  CHECK_STATUS_OK(aon_timer_testutils_get_us_from_aon_cycles(
41  kMeasurementsPerRound, &delay_micros));
42 
43  CHECK_DIF_OK(dif_clkmgr_init(
45  CHECK_DIF_OK(dif_sensor_ctrl_init(
47  &sensor_ctrl));
48  CHECK_DIF_OK(dif_rstmgr_init(
50 
51  LOG_INFO("TEST: wait for ast init");
52  IBEX_SPIN_FOR(sensor_ctrl_ast_init_done(&sensor_ctrl), 1000);
53  LOG_INFO("TEST: done ast init");
54 
55  if (UNWRAP(
56  rstmgr_testutils_reset_info_any(&rstmgr, kDifRstmgrResetInfoPor))) {
57  LOG_INFO("POR reset");
58 
59  // Configure the counters to trigger an error by setting them for external
60  // clocks.
61  CHECK_STATUS_OK(
62  clkmgr_testutils_enable_clock_counts_with_expected_thresholds(
63  &clkmgr, /*jitter_enabled=*/false, /*external_clk=*/true,
64  /*low_speed=*/true));
65  busy_spin_micros(delay_micros);
66 
67  // Check we get errors, but let the counters keep going.
69  CHECK_DIF_OK(dif_clkmgr_recov_err_code_get_codes(&clkmgr, &err_codes));
70  CHECK(err_codes != 0);
71 
72  // Trigger a rstmgr SW reset.
73  CHECK_DIF_OK(dif_rstmgr_software_device_reset(&rstmgr));
74  } else if (UNWRAP(rstmgr_testutils_reset_info_any(&rstmgr,
76  LOG_INFO("Back from rstmgr SW reset");
77  bool all_disabled = UNWRAP(clkmgr_testutils_check_measurement_enables(
78  &clkmgr, kDifToggleDisabled));
79  CHECK(all_disabled);
80 
81  CHECK_STATUS_OK(clkmgr_testutils_check_measurement_counts(&clkmgr));
82  return true;
83  } else {
85  CHECK_DIF_OK(dif_rstmgr_reset_info_get(&rstmgr, &rst_info));
86  LOG_ERROR("Unexpected rst_info 0x%x", rst_info);
87  return false;
88  }
89  return true;
90 }