Skip to content

Commit

Permalink
[dma,dv] Top-level abort test
Browse files Browse the repository at this point in the history
Co-authored-by: James Wainwright <[email protected]>
Signed-off-by: Robert Schilling <[email protected]>
(cherry picked from commit f012ecf)
  • Loading branch information
Razer6 authored and jwnrt committed Feb 18, 2025
1 parent 65ca1ba commit b71252f
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
19 changes: 19 additions & 0 deletions sw/device/tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7453,3 +7453,22 @@ opentitan_test(
"//sw/device/lib/testing/test_framework:ottf_main",
],
)

opentitan_test(
name = "dma_abort",
srcs = ["dma_abort.c"],
exec_env = {
"//hw/top_darjeeling:sim_dv": None,
},
deps = [
"//sw/device/lib/base:macros",
"//sw/device/lib/base:mmio",
"//sw/device/lib/dif:dma",
"//sw/device/lib/dif:spi_host",
"//sw/device/lib/runtime:log",
"//sw/device/lib/runtime:print",
"//sw/device/lib/testing:dma_testutils",
"//sw/device/lib/testing:pinmux_testutils",
"//sw/device/lib/testing/test_framework:ottf_main",
],
)
107 changes: 107 additions & 0 deletions sw/device/tests/dma_abort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright lowRISC contributors (OpenTitan project).
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

#include "sw/device/lib/arch/device.h"
#include "sw/device/lib/base/mmio.h"
#include "sw/device/lib/dif/dif_base.h"
#include "sw/device/lib/dif/dif_dma.h"
#include "sw/device/lib/dif/dif_spi_host.h"
#include "sw/device/lib/runtime/hart.h"
#include "sw/device/lib/runtime/log.h"
#include "sw/device/lib/testing/dma_testutils.h"
#include "sw/device/lib/testing/pinmux_testutils.h"
#include "sw/device/lib/testing/rand_testutils.h"
#include "sw/device/lib/testing/test_framework/check.h"
#include "sw/device/lib/testing/test_framework/ottf_main.h"
#include "sw/device/lib/testing/test_framework/status.h"

#include "hw/top_darjeeling/sw/autogen/top_darjeeling.h"
#include "spi_host_regs.h" // Generated.

// The TX_SIZE must be in sync with the data size in spi_device_dma_seq.sv
// 1 SPI segment can only transfer at maximum 512 bytes
#define TX_SIZE 512
#define CHUNK_SIZE 32 * 4 // Half the SPI host FIFO size

OTTF_DEFINE_TEST_CONFIG();

enum {
kSoftwareBarrierTimeoutUsec = 500,
};

// This location will be update from SV
static volatile const uint8_t kSoftwareBarrier = 0;

// Expected digest value gets backdoor'ed from the hardware
// Although not used, we need to keep it here as the shared vseq
// wants to write it.
static volatile uint32_t kShaDigestExpData[16];
static volatile uint8_t kShaMode;

uint32_t digest[16];
uint8_t received_data[TX_SIZE] __attribute__((aligned(4)));

static dif_spi_host_t spi_host;
static dif_pinmux_t pinmux;
static dif_dma_t dma;

bool test_main(void) {
// Initialize the pinmux.
CHECK_DIF_OK(dif_pinmux_init(
mmio_region_from_addr(TOP_DARJEELING_PINMUX_AON_BASE_ADDR), &pinmux));
pinmux_testutils_init(&pinmux);

// Initialise DMA.
CHECK_DIF_OK(
dif_dma_init(mmio_region_from_addr(TOP_DARJEELING_DMA_BASE_ADDR), &dma));

// Setup pinmux if required, enable weak pull-up on relevant pads
setup_pads_spi_host0(&pinmux); // direct

// Setup spi host configuration
CHECK_DIF_OK(dif_spi_host_init(
mmio_region_from_addr(TOP_DARJEELING_SPI_HOST0_BASE_ADDR), &spi_host));
init_spi_host(&spi_host, (uint32_t)kClockFreqHiSpeedPeripheralHz,
CHUNK_SIZE / 4);

// DV sync message
LOG_INFO("spi host configuration complete");

// Dummy assignment to avoid any unused variable warnings
kShaDigestExpData[0] = 0;

setup_spi_dma_transaction(&spi_host, &dma, &received_data[0], CHUNK_SIZE,
TX_SIZE);

CHECK_DIF_OK(dif_dma_start(&dma, kShaMode));

// Wait until the DMA has started to abort the transfer
IBEX_SPIN_FOR(kSoftwareBarrier == 1, kSoftwareBarrierTimeoutUsec);

CHECK_DIF_OK(dif_dma_abort(&dma));

dif_dma_status_t status;
CHECK_DIF_OK(dif_dma_status_get(&dma, &status));

CHECK(status & kDifDmaStatusAborted, "Abort bit not set");

// Reset and re-init the SPI
init_spi_host(&spi_host, (uint32_t)kClockFreqHiSpeedPeripheralHz,
CHUNK_SIZE / 4);
LOG_INFO("spi host re-configuration complete");

// Do the second transaction
setup_spi_dma_transaction(&spi_host, &dma, &received_data[0], CHUNK_SIZE,
TX_SIZE);
CHECK_DIF_OK(dif_dma_start(&dma, kShaMode));
CHECK_DIF_OK(dif_dma_status_poll(&dma, kDifDmaStatusDone));

CHECK_DIF_OK(dif_dma_sha2_digest_get(&dma, kShaMode, digest));

uint32_t digest_len;
CHECK_DIF_OK(dif_dma_get_digest_length(kShaMode, &digest_len));
CHECK_ARRAYS_EQ((uint8_t *)digest, (uint8_t *)kShaDigestExpData, digest_len);

return true;
}

0 comments on commit b71252f

Please sign in to comment.