[target/msm7630]: Add support for smem version 4.

Add platform_version variable to the new  SOC v4 structure.
Add additional variable for alignment.

Change-Id: I6e677b04b704d3887c703061644b4e636e9c0f4a
diff --git a/app/nandwrite/nandwrite.c b/app/nandwrite/nandwrite.c
index d040ff6..767f31f 100644
--- a/app/nandwrite/nandwrite.c
+++ b/app/nandwrite/nandwrite.c
@@ -72,16 +72,37 @@
 #ifdef PLATFORM_MSM7X30
     /* For 7x30, override the destination RAM address based on memory type */
     /* so the image is loaded to the larger memory segment.                */
-    struct smem_board_info board_info;
-    unsigned int board_info_struct_len = sizeof(board_info);
+    struct smem_board_info_v4 board_info_v4;
+    unsigned int board_info_len = 0;
     unsigned smem_status;
     char *build_type;
+    unsigned format = 0;
 
-    smem_status = smem_read_alloc_entry(SMEM_BOARD_INFO_LOCATION,
-                                        &board_info, board_info_struct_len );
+    smem_status = smem_read_alloc_entry_offset(SMEM_BOARD_INFO_LOCATION,
+					       &format, sizeof(format), 0);
+    if(smem_status)
+    {
+      dprintf(CRITICAL, "ERROR: unable to read shared memory for offset entry\n");
+    }
+
+    if ((format == 3) || (format == 4))
+    {
+        if (format == 4)
+	    board_info_len = sizeof(board_info_v4);
+	else
+	    board_info_len = sizeof(board_info_v4.board_info_v3);
+
+        smem_status = smem_read_alloc_entry(SMEM_BOARD_INFO_LOCATION,
+					&board_info_v4, board_info_len);
+        if(smem_status)
+        {
+            dprintf(CRITICAL, "ERROR: unable to read shared memory for Hardware Platform\n");
+        }
+    }
+
     if(!smem_status)
     {
-        build_type  = (char *)(board_info.build_id) + 8;
+        build_type  = (char *)(board_info_v4.board_info_v3.build_id) + 8;
         if (*build_type == 'A')
         {
             /* LPDDR2 configuration */
diff --git a/platform/msm_shared/smem.c b/platform/msm_shared/smem.c
index 3edc986..1d05597 100644
--- a/platform/msm_shared/smem.c
+++ b/platform/msm_shared/smem.c
@@ -63,3 +63,27 @@
 
 	return 0;
 }
+
+unsigned smem_read_alloc_entry_offset(smem_mem_type_t type, void *buf, int len, int offset)
+{
+  struct smem_alloc_info *ainfo;
+  unsigned *dest = buf;
+  unsigned src;
+  unsigned size = len;
+
+  if (((len & 0x3) != 0) || (((unsigned)buf & 0x3) != 0))
+    return 1;
+
+  if (type < SMEM_FIRST_VALID_TYPE || type > SMEM_LAST_VALID_TYPE)
+    return 1;
+
+  ainfo = &smem->alloc_info[type];
+  if (readl(&ainfo->allocated) == 0)
+    return 1;
+
+  src = MSM_SHARED_BASE + readl(&ainfo->offset) + offset;
+  for (; size > 0; src += 4, size -= 4)
+    *(dest++) = readl(src);
+
+  return 0;
+}
diff --git a/platform/msm_shared/smem.h b/platform/msm_shared/smem.h
index 8300b55..085fa9e 100644
--- a/platform/msm_shared/smem.h
+++ b/platform/msm_shared/smem.h
@@ -64,7 +64,7 @@
 	struct smem_alloc_info		alloc_info[128];
 };
 
-struct smem_board_info
+struct smem_board_info_v3
 {
     unsigned format;
     unsigned msm_id;
@@ -75,6 +75,13 @@
     unsigned hw_platform;
 };
 
+struct smem_board_info_v4
+{
+    struct smem_board_info_v3 board_info_v3;
+    unsigned platform_version;
+    unsigned buffer_align; //Need for 8 bytes alignment while reading from shared memory.
+};
+
 typedef enum {
 	SMEM_SPINLOCK_ARRAY = 7,
 
diff --git a/target/msm7630_surf/atags.c b/target/msm7630_surf/atags.c
index cd4139d..28aaab5 100644
--- a/target/msm7630_surf/atags.c
+++ b/target/msm7630_surf/atags.c
@@ -41,26 +41,42 @@
 
 int target_is_msm7x30_lpddr1(void)
 {
-    struct smem_board_info board_info;
-    unsigned int board_info_struct_len = sizeof(board_info);
+    struct smem_board_info_v4 board_info_v4;
+    unsigned int board_info_len = 0;
     unsigned smem_status;
     char *build_type;
+    unsigned format = 0;
 
     if (msm7x30_lpddr1 != -1)
     {
         return msm7x30_lpddr1;
     }
 
-    smem_status = smem_read_alloc_entry(SMEM_BOARD_INFO_LOCATION,
-					&board_info, board_info_struct_len );
+    smem_status = smem_read_alloc_entry_offset(SMEM_BOARD_INFO_LOCATION,
+					       &format, sizeof(format), 0);
     if(smem_status)
     {
-      dprintf(CRITICAL, "ERROR: unable to read shared memory for build id\n");
+      dprintf(CRITICAL, "ERROR: unable to read shared memory for offset entry\n");
+    }
+
+    if ((format == 3) || (format == 4))
+    {
+        if (format == 4)
+	    board_info_len = sizeof(board_info_v4);
+	else
+	    board_info_len = sizeof(board_info_v4.board_info_v3);
+
+        smem_status = smem_read_alloc_entry(SMEM_BOARD_INFO_LOCATION,
+					&board_info_v4, board_info_len);
+        if(smem_status)
+        {
+            dprintf(CRITICAL, "ERROR: unable to read shared memory for Hardware Platform\n");
+        }
     }
 
     msm7x30_lpddr1 = 1;
 
-    build_type  = (char *)(board_info.build_id) + 8;
+    build_type  = (char *)(board_info_v4.board_info_v3.build_id) + 8;
     if (*build_type == 'A')
     {
         msm7x30_lpddr1 = 0;
diff --git a/target/msm7630_surf/init.c b/target/msm7630_surf/init.c
index 75ac749..165af3e 100644
--- a/target/msm7630_surf/init.c
+++ b/target/msm7630_surf/init.c
@@ -56,6 +56,7 @@
 #define NUM_PAGES_PER_BLOCK    0x40
 
 static struct ptable flash_ptable;
+static int hw_platform_type = -1;
 
 /* for these partitions, start will be offset by either what we get from
  * smem, or from the above offset if smem is not useful. Also, we should
@@ -106,6 +107,7 @@
 
 static int emmc_boot = -1;  /* set to uninitialized */
 int target_is_emmc_boot(void);
+static int platform_version = -1;
 
 void target_init(void)
 {
@@ -174,36 +176,62 @@
 	flash_set_ptable(&flash_ptable);
 }
 
+int target_platform_version(void)
+{
+    return platform_version;
+}
+
 unsigned board_machtype(void)
 {
-    struct smem_board_info board_info;
-    unsigned int board_info_struct_len = sizeof(board_info);
+    struct smem_board_info_v4 board_info_v4;
+    unsigned int board_info_len = 0;
     enum platform platform_type = 0;
     unsigned smem_status;
+    unsigned format = 0;
+    if(hw_platform_type != -1)
+        return hw_platform_type;
 
-    smem_status = smem_read_alloc_entry(SMEM_BOARD_INFO_LOCATION,
-					&board_info, board_info_struct_len );
+    smem_status = smem_read_alloc_entry_offset(SMEM_BOARD_INFO_LOCATION,
+					       &format, sizeof(format), 0);
     if(smem_status)
     {
-      dprintf(CRITICAL, "ERROR: unable to read shared memory for Hardware Platform\n");
+      dprintf(CRITICAL, "ERROR: unable to read shared memory for offset entry\n");
     }
 
-    if (board_info.format == 3)
+    if ((format == 3) || (format == 4))
     {
-        platform_type = board_info.hw_platform;
+        if (format == 4)
+	    board_info_len = sizeof(board_info_v4);
+	else
+	    board_info_len = sizeof(board_info_v4.board_info_v3);
+
+        smem_status = smem_read_alloc_entry(SMEM_BOARD_INFO_LOCATION,
+					&board_info_v4, board_info_len);
+        if(smem_status)
+        {
+            dprintf(CRITICAL, "ERROR: unable to read shared memory for Hardware Platform\n");
+        }
+
+	if(format == 4)
+	    platform_version = board_info_v4.platform_version;
+
+        platform_type = board_info_v4.board_info_v3.hw_platform;
         switch (platform_type)
 	{
 	case HW_PLATFORM_SURF:
-	    return LINUX_MACHTYPE_SURF;
+	    hw_platform_type = LINUX_MACHTYPE_SURF;    break;
 	case HW_PLATFORM_FFA:
-	    return LINUX_MACHTYPE_FFA;
+	    hw_platform_type = LINUX_MACHTYPE_FFA;    break;
 	case HW_PLATFORM_FLUID:
-	    return LINUX_MACHTYPE_FLUID;
+	    hw_platform_type = LINUX_MACHTYPE_FLUID;  break;
 	default:
-            return LINUX_MACHTYPE_SURF;
+            hw_platform_type = LINUX_MACHTYPE_SURF;
 	}
+	return hw_platform_type;
     }
-    return LINUX_MACHTYPE_SURF;
+
+    hw_platform_type = LINUX_MACHTYPE_SURF;
+    return hw_platform_type;
 }
 
 void reboot_device(unsigned reboot_reason)