Platform: Make possible to set flash device for NV counters

If a platform uses the template NV counters implementation and have
more than one Flash device it can happen that firmware images and
NV counters are allocated to different flash areas. So the flash
device for NV counters can be set by the NV_COUNTERS_FLASH_DEV_NAME
macro. If it is not set, the FLASH_DEV_NAME macro will be used as the
flash device.

Change-Id: I2caf2984218994d8e79ed3f0cfba5a959c3f76b4
Signed-off-by: Mark Horvath <mark.horvath@arm.com>
diff --git a/platform/ext/common/template/nv_counters.c b/platform/ext/common/template/nv_counters.c
index 5149d0e..2764644 100644
--- a/platform/ext/common/template/nv_counters.c
+++ b/platform/ext/common/template/nv_counters.c
@@ -43,8 +43,12 @@
 #error "TFM_NV_COUNTERS_SECTOR_SIZE must be defined in flash_layout.h"
 #endif
 
-#ifndef FLASH_DEV_NAME
-#error "FLASH_DEV_NAME must be defined in flash_layout.h"
+#ifndef NV_COUNTERS_FLASH_DEV_NAME
+    #ifndef FLASH_DEV_NAME
+    #error "NV_COUNTERS_FLASH_DEV_NAME or FLASH_DEV_NAME must be defined in flash_layout.h"
+    #else
+    #define NV_COUNTERS_FLASH_DEV_NAME FLASH_DEV_NAME
+    #endif
 #endif
 /* End of compilation time checks to be sure the defines are well defined */
 
@@ -66,7 +70,7 @@
 };
 
 /* Import the CMSIS flash device driver */
-extern ARM_DRIVER_FLASH FLASH_DEV_NAME;
+extern ARM_DRIVER_FLASH NV_COUNTERS_FLASH_DEV_NAME;
 
 enum tfm_plat_err_t tfm_plat_init_nv_counter(void)
 {
@@ -74,7 +78,7 @@
     uint32_t i;
     struct nv_counters_t nv_counters = {{0}};
 
-    err = FLASH_DEV_NAME.Initialize(NULL);
+    err = NV_COUNTERS_FLASH_DEV_NAME.Initialize(NULL);
     if (err != ARM_DRIVER_OK) {
         return TFM_PLAT_ERR_SYSTEM_ERR;
     }
@@ -82,8 +86,9 @@
     /* Read the NV counter area to be able to erase the sector and write later
      * in the flash.
      */
-    err = FLASH_DEV_NAME.ReadData(TFM_NV_COUNTERS_AREA_ADDR, &nv_counters,
-                                  TFM_NV_COUNTERS_AREA_SIZE);
+    err = NV_COUNTERS_FLASH_DEV_NAME.ReadData(TFM_NV_COUNTERS_AREA_ADDR,
+                                              &nv_counters,
+                                              TFM_NV_COUNTERS_AREA_SIZE);
     if (err != ARM_DRIVER_OK) {
         return TFM_PLAT_ERR_SYSTEM_ERR;
     }
@@ -103,14 +108,15 @@
     }
 
     /* Erase sector before write in it */
-    err = FLASH_DEV_NAME.EraseSector(TFM_NV_COUNTERS_SECTOR_ADDR);
+    err = NV_COUNTERS_FLASH_DEV_NAME.EraseSector(TFM_NV_COUNTERS_SECTOR_ADDR);
     if (err != ARM_DRIVER_OK) {
         return TFM_PLAT_ERR_SYSTEM_ERR;
     }
 
     /* Write in flash the in-memory NV counter content after modification */
-    err = FLASH_DEV_NAME.ProgramData(TFM_NV_COUNTERS_AREA_ADDR, &nv_counters,
-                                     TFM_NV_COUNTERS_AREA_SIZE);
+    err = NV_COUNTERS_FLASH_DEV_NAME.ProgramData(TFM_NV_COUNTERS_AREA_ADDR,
+                                                 &nv_counters,
+                                                 TFM_NV_COUNTERS_AREA_SIZE);
     if (err != ARM_DRIVER_OK) {
         return TFM_PLAT_ERR_SYSTEM_ERR;
     }
@@ -130,7 +136,7 @@
 
     flash_addr = TFM_NV_COUNTERS_AREA_ADDR + (counter_id * NV_COUNTER_SIZE);
 
-    err = FLASH_DEV_NAME.ReadData(flash_addr, val, NV_COUNTER_SIZE);
+    err = NV_COUNTERS_FLASH_DEV_NAME.ReadData(flash_addr, val, NV_COUNTER_SIZE);
     if (err != ARM_DRIVER_OK) {
         return TFM_PLAT_ERR_SYSTEM_ERR;
     }
@@ -147,8 +153,9 @@
     /* Read the NV counter area to be able to erase the sector and write later
      * in the flash.
      */
-    err = FLASH_DEV_NAME.ReadData(TFM_NV_COUNTERS_AREA_ADDR, &nv_counters,
-                                  TFM_NV_COUNTERS_AREA_SIZE);
+    err = NV_COUNTERS_FLASH_DEV_NAME.ReadData(TFM_NV_COUNTERS_AREA_ADDR,
+                                              &nv_counters,
+                                              TFM_NV_COUNTERS_AREA_SIZE);
     if (err != ARM_DRIVER_OK) {
         return TFM_PLAT_ERR_SYSTEM_ERR;
     }
@@ -162,15 +169,16 @@
         }
 
         /* Erase sector before write in it */
-        err = FLASH_DEV_NAME.EraseSector(TFM_NV_COUNTERS_SECTOR_ADDR);
+        err = NV_COUNTERS_FLASH_DEV_NAME.EraseSector(
+                                                TFM_NV_COUNTERS_SECTOR_ADDR);
         if (err != ARM_DRIVER_OK) {
             return TFM_PLAT_ERR_SYSTEM_ERR;
         }
 
         /* Write in flash the in-memory NV counter content after modification */
-        err = FLASH_DEV_NAME.ProgramData(TFM_NV_COUNTERS_AREA_ADDR,
-                                         &nv_counters,
-                                         TFM_NV_COUNTERS_AREA_SIZE);
+        err = NV_COUNTERS_FLASH_DEV_NAME.ProgramData(TFM_NV_COUNTERS_AREA_ADDR,
+                                                     &nv_counters,
+                                                     TFM_NV_COUNTERS_AREA_SIZE);
         if (err != ARM_DRIVER_OK) {
             return TFM_PLAT_ERR_SYSTEM_ERR;
         }