Software APIs
clkmgr_jitter_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/test_framework/check.h"
10 
11 #include "clkmgr_regs.h"
12 
13 OTTF_DEFINE_TEST_CONFIG();
14 
15 void test_jitter_locked(const dif_clkmgr_t *clkmgr,
16  dif_toggle_t initial_state) {
17  // Lock jitter enable.
18  CHECK_DIF_OK(dif_clkmgr_lock_jitter_enable(clkmgr));
19  // To make sure the write is performed the dif function must be bypassed
20  // since it blocks the write if regwen is off.
21  dif_toggle_t opposite_state = initial_state == kDifToggleEnabled
24  multi_bit_bool_t opposite_mubi_state = opposite_state == kDifToggleEnabled
25  ? kMultiBitBool4True
26  : kMultiBitBool4False;
27  mmio_region_write32(clkmgr->base_addr, CLKMGR_JITTER_ENABLE_REG_OFFSET,
28  opposite_mubi_state);
29 
30  dif_toggle_t actual_state = opposite_state;
31  CHECK_DIF_OK(dif_clkmgr_jitter_get_enabled(clkmgr, &actual_state));
32  CHECK(actual_state == initial_state);
33 }
34 
35 bool test_main(void) {
36  dif_clkmgr_t clkmgr;
37  CHECK_DIF_OK(dif_clkmgr_init_from_dt(kDtClkmgrAon, &clkmgr));
38 
39  // Get the initial jitter state. It might be enabled or disabled depending
40  // on reset behavior - either is fine for the purposes of this test.
41  dif_toggle_t state;
42  CHECK_DIF_OK(dif_clkmgr_jitter_get_enabled(&clkmgr, &state));
43 
44  bool locked;
45  CHECK_DIF_OK(dif_clkmgr_jitter_enable_is_locked(&clkmgr, &locked));
46 
47  // If it is unlocked check the state can be modified.
48  if (!locked) {
49  // Toggle the enable twice so that it ends up in its original state.
50  for (int j = 0; j < 2; ++j) {
51  dif_toggle_t expected_state =
53  dif_toggle_t actual_state = state;
54  CHECK_DIF_OK(dif_clkmgr_jitter_set_enabled(&clkmgr, expected_state));
55  CHECK_DIF_OK(dif_clkmgr_jitter_get_enabled(&clkmgr, &actual_state));
56  CHECK(actual_state == expected_state);
57  state = actual_state;
58  }
59  }
60  // Check jitter_regwen disabled, so jitter_enable is locked. Do it at the end
61  // since jitter_regwen will remain locked until reset.
62  test_jitter_locked(&clkmgr, state);
63  return true;
64 }