diff --git a/hw/top_earlgrey/data/chip_testplan.hjson b/hw/top_earlgrey/data/chip_testplan.hjson index 4882693e24900..10b3169e0b314 100644 --- a/hw/top_earlgrey/data/chip_testplan.hjson +++ b/hw/top_earlgrey/data/chip_testplan.hjson @@ -3349,6 +3349,7 @@ "chip_sw_rstmgr_smoketest", "chip_sw_sram_ctrl_smoketest", "chip_sw_uart_smoketest", + "chip_sw_flash_scrambling_smoketest", ] } { @@ -3518,7 +3519,7 @@ in #14095 for more details on how to approach the implementation. ''' stage: V3 - tests: [] + tests: ["chip_sw_power_virus"] } { name: chip_sw_power_idle_load diff --git a/hw/top_earlgrey/dv/chip_sim_cfg.hjson b/hw/top_earlgrey/dv/chip_sim_cfg.hjson index af2060e3b9203..4e4c396570b07 100644 --- a/hw/top_earlgrey/dv/chip_sim_cfg.hjson +++ b/hw/top_earlgrey/dv/chip_sim_cfg.hjson @@ -1802,6 +1802,17 @@ ] run_timeout_mins: 300 } + { + name: chip_sw_flash_scrambling_smoketest + uvm_test_seq: chip_sw_base_vseq + sw_images: [ + "//sw/device/tests:flash_scrambling_smoketest:1", + "//sw/device/tests:flash_scrambling_smoketest_otp_img_rma:4", + ] + en_run_modes: ["sw_test_mode_test_rom"] + run_opts: ["+use_otp_image=OtpTypeCustom"] + run_timeout_mins: 10 + } ] // List of regressions. @@ -1822,6 +1833,7 @@ "chip_sw_spi_device_pass_through", "chip_sw_i2c_host_tx_rx", "chip_sw_i2c_device_tx_rx", + "chip_sw_flash_scrambling_smoketest", "chip_plic_all_irqs", "chip_sw_example_flash", "chip_sw_example_rom", diff --git a/sw/device/lib/testing/test_rom/test_rom.c b/sw/device/lib/testing/test_rom/test_rom.c index c949eee55ead0..c9adbffa2045a 100644 --- a/sw/device/lib/testing/test_rom/test_rom.c +++ b/sw/device/lib/testing/test_rom/test_rom.c @@ -113,6 +113,25 @@ bool rom_test_main(void) { mmio_region_from_addr(TOP_EARLGREY_FLASH_CTRL_CORE_BASE_ADDR))); CHECK_DIF_OK(dif_flash_ctrl_start_controller_init(&flash_ctrl)); flash_ctrl_testutils_wait_for_init(&flash_ctrl); +#if !OT_IS_ENGLISH_BREAKFAST + // Check the otp to see if flash scramble should be enabled. + otp_val = abs_mmio_read32( + TOP_EARLGREY_OTP_CTRL_CORE_BASE_ADDR + OTP_CTRL_SW_CFG_WINDOW_REG_OFFSET + + OTP_CTRL_PARAM_CREATOR_SW_CFG_FLASH_DATA_DEFAULT_CFG_OFFSET); + if (otp_val != 0) { + dif_flash_ctrl_region_properties_t default_properties; + CHECK_DIF_OK(dif_flash_ctrl_get_default_region_properties( + &flash_ctrl, &default_properties)); + default_properties.scramble_en = + bitfield_field32_read(otp_val, FLASH_CTRL_OTP_FIELD_SCRAMBLING); + default_properties.ecc_en = + bitfield_field32_read(otp_val, FLASH_CTRL_OTP_FIELD_ECC); + default_properties.high_endurance_en = + bitfield_field32_read(otp_val, FLASH_CTRL_OTP_FIELD_HE); + CHECK_DIF_OK(dif_flash_ctrl_set_default_region_properties( + &flash_ctrl, default_properties)); + } +#endif CHECK_DIF_OK( dif_flash_ctrl_set_flash_enablement(&flash_ctrl, kDifToggleEnabled)); @@ -171,27 +190,6 @@ bool rom_test_main(void) { LOG_INFO("Jitter is enabled"); } -#if !OT_IS_ENGLISH_BREAKFAST - // Check the otp to see if flash scramble should be enabled. - otp_val = abs_mmio_read32( - TOP_EARLGREY_OTP_CTRL_CORE_BASE_ADDR + OTP_CTRL_SW_CFG_WINDOW_REG_OFFSET + - OTP_CTRL_PARAM_CREATOR_SW_CFG_FLASH_DATA_DEFAULT_CFG_OFFSET); - - if (otp_val != 0) { - dif_flash_ctrl_region_properties_t default_properties; - CHECK_DIF_OK(dif_flash_ctrl_get_default_region_properties( - &flash_ctrl, &default_properties)); - default_properties.scramble_en = - bitfield_field32_read(otp_val, FLASH_CTRL_OTP_FIELD_SCRAMBLING); - default_properties.ecc_en = - bitfield_field32_read(otp_val, FLASH_CTRL_OTP_FIELD_ECC); - default_properties.high_endurance_en = - bitfield_field32_read(otp_val, FLASH_CTRL_OTP_FIELD_HE); - CHECK_DIF_OK(dif_flash_ctrl_set_default_region_properties( - &flash_ctrl, default_properties)); - } -#endif - if (bootstrap_requested() == kHardenedBoolTrue) { // This log statement is used to synchronize the rom and DV testbench // for specific test cases. diff --git a/sw/device/tests/BUILD b/sw/device/tests/BUILD index 6153b521b6d5d..cab2a6e96eaff 100644 --- a/sw/device/tests/BUILD +++ b/sw/device/tests/BUILD @@ -2100,10 +2100,8 @@ otp_json( otp_partition( name = "CREATOR_SW_CFG", items = { - "CREATOR_SW_CFG_FLASH_DATA_DEFAULT_CFG": "0000090909", - # TODO(#14814): enable flash scrambling / ECC by replacing above - # with below. - #"CREATOR_SW_CFG_FLASH_DATA_DEFAULT_CFG": "0000090606", + # Enable flash scrambling and ECC. + "CREATOR_SW_CFG_FLASH_DATA_DEFAULT_CFG": "0000090606", }, ), ], @@ -2231,3 +2229,37 @@ opentitan_functest( "//sw/device/lib/testing/test_framework:ujson_ottf", ], ) + +otp_json( + name = "flash_scrambling_smoketest_otp_overlay", + partitions = [ + otp_partition( + name = "CREATOR_SW_CFG", + items = { + # Enable flash scrambling and ECC. + "CREATOR_SW_CFG_FLASH_DATA_DEFAULT_CFG": "0000090606", + }, + ), + ], +) + +otp_image( + name = "flash_scrambling_smoketest_otp_img_rma", + src = "//hw/ip/otp_ctrl/data:otp_json_rma", + overlays = STD_OTP_OVERLAYS + [":flash_scrambling_smoketest_otp_overlay"], + visibility = ["//visibility:private"], +) + +opentitan_functest( + name = "flash_scrambling_smoketest", + srcs = ["example_test_from_flash.c"], + dv = dv_params( + otp = ":flash_scrambling_smoketest_otp_img_rma", + ), + targets = [ + "dv", + ], + deps = [ + "//sw/device/lib/testing/test_framework:ottf_main", + ], +) diff --git a/sw/device/tests/power_virus_systemtest.c b/sw/device/tests/power_virus_systemtest.c index ee46939a3b456..79612f5d90a16 100644 --- a/sw/device/tests/power_virus_systemtest.c +++ b/sw/device/tests/power_virus_systemtest.c @@ -1157,8 +1157,8 @@ static void check_otp_csr_configs(void) { dif_flash_ctrl_region_properties_t default_properties; CHECK_DIF_OK(dif_flash_ctrl_get_default_region_properties( &flash_ctrl, &default_properties)); - CHECK(default_properties.scramble_en == kMultiBitBool4False); - CHECK(default_properties.ecc_en == kMultiBitBool4False); + CHECK(default_properties.scramble_en == kMultiBitBool4True); + CHECK(default_properties.ecc_en == kMultiBitBool4True); CHECK(default_properties.high_endurance_en == kMultiBitBool4False); } diff --git a/util/design/gen-flash-img.py b/util/design/gen-flash-img.py index c8e1eba43cff3..4329a35e8fcfb 100755 --- a/util/design/gen-flash-img.py +++ b/util/design/gen-flash-img.py @@ -11,7 +11,6 @@ """ import argparse -import functools import re import sys from dataclasses import dataclass @@ -107,7 +106,6 @@ class FlashScramblingConfigs: data_key: int = None -@functools.lru_cache(maxsize=None) def _xex_scramble(data: int, word_addr: int, flash_addr_key: int, flash_data_key: int) -> int: operand_a = ((flash_addr_key & FLASH_GF_OPERAND_A_MASK) >> @@ -246,12 +244,14 @@ def _reformat_flash_vmem( line_items = line.split() reformatted_line = "" address = None + address_offset = 0 data = None for item in line_items: # Process the address first. if re.match(r"^@", item): reformatted_line += item address = int(item.lstrip("@"), 16) + address_offset = 0 # Process the data words. else: data = int(item, 16) @@ -262,7 +262,7 @@ def _reformat_flash_vmem( data_w_intg_ecc &= 0xF_FFFF_FFFF_FFFF_FFFF if scrambling_configs.scrambling_enabled: intg_ecc = data_w_intg_ecc & (0xF << FLASH_WORD_SIZE) - data = _xex_scramble(data, address, + data = _xex_scramble(data, address + address_offset, scrambling_configs.addr_key, scrambling_configs.data_key) data_w_intg_ecc = intg_ecc | data @@ -274,6 +274,7 @@ def _reformat_flash_vmem( data_w_intg_ecc) reformatted_line += str.format(VMEM_FORMAT_STR, data_w_full_ecc) + address_offset += 1 # Append reformatted line to what will be the new output VMEM file. reformatted_vmem_lines.append(reformatted_line)