opentitanlib/test_utils/
lc.rs

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
5use std::time::Duration;
6
7use anyhow::Result;
8use arrayvec::ArrayVec;
9
10use ot_hal::dif::lc_ctrl::{DifLcCtrlState, LcCtrlReg, LcCtrlStatus};
11
12use crate::app::TransportWrapper;
13use crate::io::jtag::{JtagParams, JtagTap};
14use crate::test_utils::lc_transition::wait_for_status;
15
16pub fn read_lc_state(
17    transport: &TransportWrapper,
18    jtag_params: &JtagParams,
19    reset_delay: Duration,
20) -> Result<DifLcCtrlState> {
21    transport.pin_strapping("PINMUX_TAP_LC")?.apply()?;
22
23    // Apply bootstrap pin to be able to connect to JTAG when ROM execution is
24    // enabled.
25    transport.pin_strapping("ROM_BOOTSTRAP")?.apply()?;
26    transport.reset_target(reset_delay, true)?;
27    let mut jtag = jtag_params.create(transport)?.connect(JtagTap::LcTap)?;
28    // We must wait for the lc_ctrl to initialize before the LC state is exposed.
29    wait_for_status(
30        &mut *jtag,
31        Duration::from_secs(1),
32        LcCtrlStatus::INITIALIZED,
33    )?;
34    let raw_lc_state = jtag.read_lc_ctrl_reg(&LcCtrlReg::LcState)?;
35    jtag.disconnect()?;
36    transport.pin_strapping("PINMUX_TAP_LC")?.remove()?;
37    transport.pin_strapping("ROM_BOOTSTRAP")?.remove()?;
38    DifLcCtrlState::from_redundant_encoding(raw_lc_state)
39}
40
41pub fn read_device_id(
42    transport: &TransportWrapper,
43    jtag_params: &JtagParams,
44    reset_delay: Duration,
45) -> Result<ArrayVec<u32, 8>> {
46    transport.pin_strapping("PINMUX_TAP_LC")?.apply()?;
47
48    // Apply bootstrap pin to be able to connect to JTAG when ROM execution is
49    // enabled.
50    transport.pin_strapping("ROM_BOOTSTRAP")?.apply()?;
51    transport.reset_target(reset_delay, true)?;
52    let mut jtag = jtag_params.create(transport)?.connect(JtagTap::LcTap)?;
53    // We must wait for the lc_ctrl to initialize before the LC state is exposed.
54    wait_for_status(
55        &mut *jtag,
56        Duration::from_secs(1),
57        LcCtrlStatus::INITIALIZED,
58    )?;
59
60    let mut device_id = ArrayVec::<u32, 8>::new();
61    device_id.push(jtag.read_lc_ctrl_reg(&LcCtrlReg::DeviceId0)?);
62    device_id.push(jtag.read_lc_ctrl_reg(&LcCtrlReg::DeviceId1)?);
63    device_id.push(jtag.read_lc_ctrl_reg(&LcCtrlReg::DeviceId2)?);
64    device_id.push(jtag.read_lc_ctrl_reg(&LcCtrlReg::DeviceId3)?);
65    device_id.push(jtag.read_lc_ctrl_reg(&LcCtrlReg::DeviceId4)?);
66    device_id.push(jtag.read_lc_ctrl_reg(&LcCtrlReg::DeviceId5)?);
67    device_id.push(jtag.read_lc_ctrl_reg(&LcCtrlReg::DeviceId6)?);
68    device_id.push(jtag.read_lc_ctrl_reg(&LcCtrlReg::DeviceId7)?);
69
70    jtag.disconnect()?;
71    transport.pin_strapping("PINMUX_TAP_LC")?.remove()?;
72    transport.pin_strapping("ROM_BOOTSTRAP")?.remove()?;
73
74    Ok(device_id)
75}