Software APIs
sysrst_ctrl_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/sysrst_ctrl_testutils.h"
6
8#include "sw/device/lib/testing/test_framework/check.h"
9
11
12void sysrst_ctrl_testutils_setup_dio(dif_pinmux_t *pinmux) {
13 // Make sure that the DIO pins EC reset and flash WP are configured correctly
14 // https://opentitan.org/book/hw/ip/sysrst_ctrl/doc/theory_of_operation.html#ec-and-power-on-reset
15 // https://opentitan.org/book/hw/ip/sysrst_ctrl/doc/theory_of_operation.html#flash-write-protect-output
16 //
17 // The documentation says that they should be configured as open drain. These
18 // pads only support virtual open drain.
19 dif_pinmux_pad_attr_t out_attr;
20 dif_pinmux_pad_attr_t in_attr = {
21 .slew_rate = 0,
22 .drive_strength = 0,
23 .flags = kDifPinmuxPadAttrVirtualOpenDrain,
24 };
25 CHECK_DIF_OK(
26 dif_pinmux_pad_write_attrs(pinmux, kTopEarlgreyDirectPadsSysrstCtrlEcRstL,
27 kDifPinmuxPadKindDio, in_attr, &out_attr));
28 CHECK_DIF_OK(dif_pinmux_pad_write_attrs(
29 pinmux, kTopEarlgreyDirectPadsSysrstCtrlFlashWpL, kDifPinmuxPadKindDio,
30 in_attr, &out_attr));
31}
32
33void sysrst_ctrl_testutils_release_dio(dif_sysrst_ctrl_t *sysrst_ctrl,
34 bool release_ec, bool release_flash) {
35 // Make sure that the DIO pins EC reset and flash WP are released according to
36 // the documentation:
37 // https://opentitan.org/book/hw/ip/sysrst_ctrl/doc/theory_of_operation.html#ec-and-power-on-reset
38 // https://opentitan.org/book/hw/ip/sysrst_ctrl/doc/theory_of_operation.html#flash-write-protect-output
39 // We also need to disable the output override mecanism (i.e. "release the
40 // pin").
41 if (release_ec) {
42 dif_sysrst_ctrl_pin_config_t cfg_ec_reset = {
43 .enabled = kDifToggleDisabled,
44 .allow_zero = true,
45 .allow_one = true,
46 .override_value = false,
47 };
48 CHECK_DIF_OK(dif_sysrst_ctrl_output_pin_override_configure(
49 sysrst_ctrl, kDifSysrstCtrlPinEcResetInOut, cfg_ec_reset));
50 }
51 // Confusingly, the flash WP is different: the value of flash_wp_l_o defaults
52 // to logic 0 when it is not explicitly driven via the override function.
53 // Therefore to disable the driver we need to *enable* the override and set
54 // to 1.
55 if (release_flash) {
56 dif_sysrst_ctrl_pin_config_t cfg_flash_wp = {
57 .enabled = kDifToggleEnabled,
58 .allow_zero = false,
59 .allow_one = true,
60 .override_value = true,
61 };
62 CHECK_DIF_OK(dif_sysrst_ctrl_output_pin_override_configure(
63 sysrst_ctrl, kDifSysrstCtrlPinFlashWriteProtectInOut, cfg_flash_wp));
64 }
65}
66
67void sysrst_ctrl_testutils_set_ec_rst_pulse_width(
68 dif_sysrst_ctrl_t *sysrst_ctrl, uint32_t pulse_us) {
69 // The register field counts in aon_clock ticks.
70 uint64_t ticks =
71 udiv64_slow((uint64_t)pulse_us * kClockFreqAonHz, 1000 * 1000, NULL);
72 // The register field is 16-bit wide.
73 OT_ASSERT_ENUM_VALUE(SYSRST_CTRL_EC_RST_CTL_EC_RST_PULSE_OFFSET, 0);
74 CHECK(ticks <= SYSRST_CTRL_EC_RST_CTL_EC_RST_PULSE_MASK);
75 mmio_region_write32(sysrst_ctrl->base_addr, SYSRST_CTRL_EC_RST_CTL_REG_OFFSET,
76 (uint16_t)ticks);
77}