Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test] configure SPI host in for passthrough mode in power virus test #17010

Merged
merged 4 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sw/device/lib/dif/dif_pinmux.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ typedef struct dif_pinmux_pad_attr {
*/
dif_pinmux_pad_drive_strength_t drive_strength;
/**
* A bit map of single-bit attribute flags. @sa dif_pinmux_pad_attr_flags_t
* A bit map of single-bit attribute flags. See dif_pinmux_pad_attr_flags_t
* for the mapping and definitions.
*/
dif_pinmux_pad_attr_flags_t flags;
Expand Down
13 changes: 13 additions & 0 deletions sw/device/lib/testing/pinmux_testutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,16 @@ uint32_t pinmux_testutils_read_straps(dif_pinmux_t *pinmux, dif_gpio_t *gpio) {
<< 4;
return strap;
}

void pinmux_testutils_configure_pads(const dif_pinmux_t *pinmux,
const pinmux_pad_attributes_t *attrs,
size_t num_attrs) {
for (size_t i = 0; i < num_attrs; ++i) {
dif_pinmux_pad_attr_t desired_attr, actual_attr;
CHECK_DIF_OK(dif_pinmux_pad_get_attrs(pinmux, attrs[i].pad, attrs[i].kind,
&desired_attr));
desired_attr.flags = attrs[i].flags;
CHECK_DIF_OK(dif_pinmux_pad_write_attrs(pinmux, attrs[i].pad, attrs[i].kind,
desired_attr, &actual_attr));
}
}
16 changes: 16 additions & 0 deletions sw/device/lib/testing/pinmux_testutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,20 @@ uint32_t pinmux_testutils_read_strap_pin(dif_pinmux_t *pinmux, dif_gpio_t *gpio,
*/
uint32_t pinmux_testutils_read_straps(dif_pinmux_t *pinmux, dif_gpio_t *gpio);

/**
* A convenience struct to associate pad attributes with a specific pad.
*/
typedef struct pinmux_pad_attributes {
dif_pinmux_index_t pad;
dif_pinmux_pad_kind_t kind;
dif_pinmux_pad_attr_flags_t flags;
} pinmux_pad_attributes_t;

/**
* Configures several pad attributes.
*/
void pinmux_testutils_configure_pads(const dif_pinmux_t *pinmux,
const pinmux_pad_attributes_t *attrs,
size_t num_attrs);

#endif // OPENTITAN_SW_DEVICE_LIB_TESTING_PINMUX_TESTUTILS_H_
2 changes: 2 additions & 0 deletions sw/device/tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2072,10 +2072,12 @@ opentitan_functest(
"//sw/device/lib/dif:kmac",
"//sw/device/lib/dif:pinmux",
"//sw/device/lib/dif:spi_device",
"//sw/device/lib/dif:spi_host",
"//sw/device/lib/dif:uart",
"//sw/device/lib/runtime:log",
"//sw/device/lib/testing:aes_testutils",
"//sw/device/lib/testing:entropy_testutils",
"//sw/device/lib/testing:pinmux_testutils",
"//sw/device/lib/testing:spi_device_testutils",
"//sw/device/lib/testing/test_framework:check",
"//sw/device/lib/testing/test_framework:ottf_main",
Expand Down
60 changes: 60 additions & 0 deletions sw/device/tests/power_virus_systemtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
#include "sw/device/lib/dif/dif_kmac.h"
#include "sw/device/lib/dif/dif_pinmux.h"
#include "sw/device/lib/dif/dif_spi_device.h"
#include "sw/device/lib/dif/dif_spi_host.h"
#include "sw/device/lib/dif/dif_uart.h"
#include "sw/device/lib/runtime/log.h"
#include "sw/device/lib/testing/aes_testutils.h"
#include "sw/device/lib/testing/entropy_testutils.h"
#include "sw/device/lib/testing/pinmux_testutils.h"
#include "sw/device/lib/testing/spi_device_testutils.h"
#include "sw/device/lib/testing/test_framework/check.h"
#include "sw/device/lib/testing/test_framework/ottf_macros.h"
Expand Down Expand Up @@ -49,6 +51,7 @@ static dif_i2c_t i2c_0;
static dif_i2c_t i2c_1;
static dif_i2c_t i2c_2;
static dif_spi_device_handle_t spi_device;
static dif_spi_host_t spi_host_0;
static dif_uart_t uart_1;
static dif_uart_t uart_2;
static dif_uart_t uart_3;
Expand Down Expand Up @@ -92,6 +95,37 @@ enum {
kI2c2DeviceAddress1 = 0x66,
};

/**
* Pinmux pad configurations.
*/
static const pinmux_pad_attributes_t pinmux_pad_attributes[] = {
// Enable pull-ups for spi_host_0 data pins to avoid floating inputs.
{
.pad = kTopEarlgreyDirectPadsSpiHost0Sd0,
.kind = kDifPinmuxPadKindDio,
.flags = kDifPinmuxPadAttrPullResistorEnable |
kDifPinmuxPadAttrPullResistorUp,
},
{
.pad = kTopEarlgreyDirectPadsSpiHost0Sd1,
.kind = kDifPinmuxPadKindDio,
.flags = kDifPinmuxPadAttrPullResistorEnable |
kDifPinmuxPadAttrPullResistorUp,
},
{
.pad = kTopEarlgreyDirectPadsSpiHost0Sd2,
.kind = kDifPinmuxPadKindDio,
.flags = kDifPinmuxPadAttrPullResistorEnable |
kDifPinmuxPadAttrPullResistorUp,
},
{
.pad = kTopEarlgreyDirectPadsSpiHost0Sd3,
.kind = kDifPinmuxPadKindDio,
.flags = kDifPinmuxPadAttrPullResistorEnable |
kDifPinmuxPadAttrPullResistorUp,
},
};

/**
* The mask share, used to mask kAesKey.
*/
Expand Down Expand Up @@ -151,6 +185,8 @@ static void init_peripheral_handles() {
dif_i2c_init(mmio_region_from_addr(TOP_EARLGREY_I2C2_BASE_ADDR), &i2c_2));
CHECK_DIF_OK(dif_spi_device_init_handle(
mmio_region_from_addr(TOP_EARLGREY_SPI_DEVICE_BASE_ADDR), &spi_device));
CHECK_DIF_OK(dif_spi_host_init(
mmio_region_from_addr(TOP_EARLGREY_SPI_HOST0_BASE_ADDR), &spi_host_0));
}

/**
Expand Down Expand Up @@ -393,8 +429,31 @@ static void configure_i2c(dif_i2c_t *i2c, uint8_t device_addr_0,
CHECK_DIF_OK(dif_i2c_line_loopback_set_enabled(i2c, kDifToggleEnabled));
}

static void configure_spi_host() {
// Enable pull-ups for spi_host_0 data pins to avoid floating inputs.
if (kDeviceType == kDeviceSimDV || kDeviceType == kDeviceSimVerilator) {
pinmux_testutils_configure_pads(&pinmux, pinmux_pad_attributes,
ARRAYSIZE(pinmux_pad_attributes));
}
// These values are mostly filler as spi_host_0 will be in passthrough mode.
CHECK_DIF_OK(dif_spi_host_configure(
&spi_host_0,
(dif_spi_host_config_t){
.spi_clock = kClockFreqHiSpeedPeripheralHz / 2,
.peripheral_clock_freq_hz = kClockFreqHiSpeedPeripheralHz,
.chip_select =
{
.idle = 2,
.trail = 2,
.lead = 2,
},
}));
CHECK_DIF_OK(dif_spi_host_output_set_enabled(&spi_host_0, /*enabled=*/true));
}

bool test_main(void) {
init_peripheral_handles();
pinmux_testutils_init(&pinmux);
configure_gpio_indicator_pin();
configure_adc_ctrl_to_continuously_sample();
configure_entropy_complex();
Expand All @@ -407,6 +466,7 @@ bool test_main(void) {
configure_i2c(&i2c_0, kI2c0DeviceAddress0, kI2c0DeviceAddress1);
configure_i2c(&i2c_1, kI2c1DeviceAddress0, kI2c1DeviceAddress1);
configure_i2c(&i2c_2, kI2c2DeviceAddress0, kI2c2DeviceAddress1);
configure_spi_host();
spi_device_testutils_configure_passthrough(&spi_device, /*filters=*/0,
/*upload_write_commands=*/false);
return true;
Expand Down
21 changes: 2 additions & 19 deletions sw/device/tests/sim_dv/spi_passthrough_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,6 @@ static dif_spi_device_handle_t spi_device;
static dif_spi_host_t spi_host0;
static dif_spi_host_t spi_host1;

/**
* A convenience struct to associate pad attributes with a specific pad.
*/
typedef struct pinmux_pad_attributes {
dif_pinmux_index_t pad;
dif_pinmux_pad_kind_t kind;
dif_pinmux_pad_attr_flags_t flags;
} pinmux_pad_attributes_t;

// Enable pull-ups for spi_host data pins to avoid floating inputs.
static const pinmux_pad_attributes_t pinmux_pad_config[] = {
{
Expand Down Expand Up @@ -484,16 +475,8 @@ bool test_main(void) {
CHECK_DIF_OK(dif_pinmux_init(
mmio_region_from_addr(TOP_EARLGREY_PINMUX_AON_BASE_ADDR), &pinmux));
pinmux_testutils_init(&pinmux);
for (int i = 0; i < ARRAYSIZE(pinmux_pad_config); ++i) {
dif_pinmux_pad_attr_t attr, attr_check;
pinmux_pad_attributes_t config = pinmux_pad_config[i];
CHECK_DIF_OK(
dif_pinmux_pad_get_attrs(&pinmux, config.pad, config.kind, &attr));
attr.flags = config.flags;
CHECK_DIF_OK(dif_pinmux_pad_write_attrs(&pinmux, config.pad, config.kind,
attr, &attr_check));
// Check that attributes were accepted?
}
pinmux_testutils_configure_pads(&pinmux, pinmux_pad_config,
ARRAYSIZE(pinmux_pad_config));
for (int i = 0; i < ARRAYSIZE(pinmux_in_config); ++i) {
pinmux_select_t setting = pinmux_in_config[i];
CHECK_DIF_OK(
Expand Down