11b076113Slaurenw-arm /*
2*5b46aaccSYann Gautier * Copyright (c) 2023-2025, Arm Limited and Contributors. All rights reserved.
31b076113Slaurenw-arm *
41b076113Slaurenw-arm * SPDX-License-Identifier: BSD-3-Clause
51b076113Slaurenw-arm */
61b076113Slaurenw-arm
71b076113Slaurenw-arm #include <stdint.h>
81b076113Slaurenw-arm #include <stdio.h>
91b076113Slaurenw-arm
101b076113Slaurenw-arm #include <plat/common/platform.h>
117f8589cdSTamas Ban #include "rse_platform_api.h"
121b076113Slaurenw-arm
131b076113Slaurenw-arm #include <platform_def.h>
14*5b46aaccSYann Gautier #include <tc_rse_comms.h>
151b076113Slaurenw-arm
nv_counter_test(void)1657cc12c8SSandrine Bailleux int nv_counter_test(void)
171b076113Slaurenw-arm {
181b076113Slaurenw-arm psa_status_t status;
191b076113Slaurenw-arm uint32_t old_val;
201b076113Slaurenw-arm uint32_t new_val;
211b076113Slaurenw-arm uint32_t id;
221b076113Slaurenw-arm
23*5b46aaccSYann Gautier status = plat_rse_comms_init();
241b076113Slaurenw-arm if (status != PSA_SUCCESS) {
257f8589cdSTamas Ban printf("Failed to initialize RSE communication channel - psa_status = %d\n", status);
2657cc12c8SSandrine Bailleux return -1;
271b076113Slaurenw-arm }
281b076113Slaurenw-arm
291b076113Slaurenw-arm for (id = 0; id < 3; id++) {
307f8589cdSTamas Ban status = rse_platform_nv_counter_read(id, sizeof(old_val), (uint8_t *)&old_val);
311b076113Slaurenw-arm if (status != PSA_SUCCESS) {
327f8589cdSTamas Ban printf("Failed during first id=(%d) rse_platform_nv_counter_read - psa_status = %d\n",
33cb6b7505Slaurenw-arm id, status);
3457cc12c8SSandrine Bailleux return -1;
351b076113Slaurenw-arm }
361b076113Slaurenw-arm
377f8589cdSTamas Ban status = rse_platform_nv_counter_increment(id);
381b076113Slaurenw-arm if (status != PSA_SUCCESS) {
397f8589cdSTamas Ban printf("Failed during id=(%d) rse_platform_nv_counter_increment - psa_status = %d\n",
40cb6b7505Slaurenw-arm id, status);
4157cc12c8SSandrine Bailleux return -1;
421b076113Slaurenw-arm }
431b076113Slaurenw-arm
447f8589cdSTamas Ban status = rse_platform_nv_counter_read(id, sizeof(new_val), (uint8_t *)&new_val);
451b076113Slaurenw-arm if (status != PSA_SUCCESS) {
467f8589cdSTamas Ban printf("Failed during second id=(%d) rse_platform_nv_counter_read - psa_status = %d\n",
47cb6b7505Slaurenw-arm id, status);
4857cc12c8SSandrine Bailleux return -1;
491b076113Slaurenw-arm }
501b076113Slaurenw-arm
511b076113Slaurenw-arm if (old_val + 1 != new_val) {
521b076113Slaurenw-arm printf("Failed nv_counter_test: old_val (%d) + 1 != new_val (%d)\n",
531b076113Slaurenw-arm old_val, new_val);
5457cc12c8SSandrine Bailleux return -1;
551b076113Slaurenw-arm }
561b076113Slaurenw-arm }
571b076113Slaurenw-arm printf("Passed nv_counter_test\n");
5857cc12c8SSandrine Bailleux
5957cc12c8SSandrine Bailleux return 0;
601b076113Slaurenw-arm }
61