msm8960: Clean up board detect code.

Add support for following functions to get board
information:

- board_platform_id - Returns platform id.
- board_target_id   - Returns target id.
- board_baseband    - Returns baseband type.

Change-Id: I513946fe2f0385302a13244f96bd48ae565183cf
diff --git a/platform/msm8960/platform.c b/platform/msm8960/platform.c
index c4f339f..c15f32b 100644
--- a/platform/msm8960/platform.c
+++ b/platform/msm8960/platform.c
@@ -36,6 +36,7 @@
 #include <dev/fbcon.h>
 #include <mmu.h>
 #include <arch/arm/mmu.h>
+#include <board.h>
 
 extern void platform_init_timer(void);
 extern void platform_panel_backlight_on(void);
@@ -89,6 +90,7 @@
 	msm_clocks_init();
 	qgic_init();
 	platform_init_timer();
+	board_init();
 }
 
 void platform_init(void)
diff --git a/platform/msm_shared/board.c b/platform/msm_shared/board.c
new file mode 100644
index 0000000..94bcd4c
--- /dev/null
+++ b/platform/msm_shared/board.c
@@ -0,0 +1,196 @@
+/* Copyright (c) 2012, Code Aurora Forum. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above
+ *       copyright notice, this list of conditions and the following
+ *       disclaimer in the documentation and/or other materials provided
+ *       with the distribution.
+ *     * Neither the name of Code Aurora Forum, Inc. nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <debug.h>
+#include <board.h>
+#include <smem.h>
+#include <baseband.h>
+
+static struct board_data board = {UNKNOWN,
+	HW_PLATFORM_UNKNOWN,
+	HW_PLATFORM_SUBTYPE_UNKNOWN,
+	LINUX_MACHTYPE_UNKNOWN,};
+
+static void platform_detect()
+{
+	struct smem_board_info_v6 board_info_v6;
+	unsigned int board_info_len = 0;
+	unsigned ret = 0;
+	unsigned format = 0;
+
+	ret = smem_read_alloc_entry_offset(SMEM_BOARD_INFO_LOCATION,
+						   &format, sizeof(format), 0);
+	if (ret)
+		return;
+
+	if (format == 6) {
+		board_info_len = sizeof(board_info_v6);
+
+		ret =
+			smem_read_alloc_entry(SMEM_BOARD_INFO_LOCATION,
+					  &board_info_v6,
+					  board_info_len);
+		if (ret)
+			return;
+
+		board.platform = board_info_v6.board_info_v3.msm_id;
+		board.platform_hw =
+			board_info_v6.board_info_v3.hw_platform;
+		board.platform_subtype =
+			board_info_v6.platform_subtype;
+	} else {
+		dprintf(CRITICAL, "Unsupported board info format.\n");
+	}
+}
+
+static void target_detect()
+{
+	unsigned platform_id;
+	unsigned platform_hw;
+	unsigned target_id;
+
+	platform_id = board.platform;
+	platform_hw = board.platform_hw;
+
+	/* Detect the board we are running on */
+	if ((platform_id == MSM8960) || (platform_id == MSM8660A)
+	    || (platform_id == MSM8260A) || (platform_id == APQ8060A)) {
+		switch (platform_hw) {
+		case HW_PLATFORM_SURF:
+			target_id = LINUX_MACHTYPE_8960_CDP;
+			break;
+		case HW_PLATFORM_MTP:
+			target_id = LINUX_MACHTYPE_8960_MTP;
+			break;
+		case HW_PLATFORM_FLUID:
+			target_id = LINUX_MACHTYPE_8960_FLUID;
+			break;
+		case HW_PLATFORM_LIQUID:
+			target_id = LINUX_MACHTYPE_8960_LIQUID;
+			break;
+		default:
+			target_id = LINUX_MACHTYPE_8960_CDP;
+		}
+	} else if ((platform_id == MSM8230) || (platform_id == MSM8630)
+		   || (platform_id == MSM8930) || (platform_id == APQ8030)) {
+		switch (platform_hw) {
+		case HW_PLATFORM_SURF:
+			target_id = LINUX_MACHTYPE_8930_CDP;
+			break;
+		case HW_PLATFORM_MTP:
+			target_id = LINUX_MACHTYPE_8930_MTP;
+			break;
+		case HW_PLATFORM_FLUID:
+			target_id = LINUX_MACHTYPE_8930_FLUID;
+			break;
+		default:
+			target_id = LINUX_MACHTYPE_8930_CDP;
+		}
+	} else if ((platform_id == MSM8227) || (platform_id == MSM8627)) {
+		switch (platform_hw) {
+		case HW_PLATFORM_SURF:
+			target_id = LINUX_MACHTYPE_8627_CDP;
+			break;
+		case HW_PLATFORM_MTP:
+			target_id = LINUX_MACHTYPE_8627_MTP;
+			break;
+		default:
+			target_id = LINUX_MACHTYPE_8627_CDP;
+		}
+	} else if ((platform_id == APQ8064) || (platform_id == MPQ8064)) {
+		switch (platform_hw) {
+		case HW_PLATFORM_SURF:
+			target_id = LINUX_MACHTYPE_8064_CDP;
+			break;
+		case HW_PLATFORM_MTP:
+			target_id = LINUX_MACHTYPE_8064_MTP;
+			break;
+		case HW_PLATFORM_LIQUID:
+			target_id = LINUX_MACHTYPE_8064_LIQUID;
+			break;
+		case HW_PLATFORM_HRD:
+			target_id = LINUX_MACHTYPE_8064_HRD;
+			break;
+		case HW_PLATFORM_DTV:
+			target_id = LINUX_MACHTYPE_8064_DTV;
+			break;
+		default:
+			target_id = LINUX_MACHTYPE_8064_CDP;
+		}
+	} else {
+		dprintf(CRITICAL, "platform_id (%d) is not identified.\n",
+			platform_id);
+		ASSERT(0);
+	}
+	board.target = target_id;
+}
+
+static void baseband_detect()
+{
+	unsigned baseband = BASEBAND_MSM;
+	unsigned platform_subtype;
+	unsigned platform_id;
+
+	platform_id = board.platform;
+	platform_subtype = board.platform_subtype;
+
+	/* Check for MDM or APQ baseband variants.  Default to MSM */
+	if (platform_subtype == HW_PLATFORM_SUBTYPE_MDM)
+		baseband = BASEBAND_MDM;
+	else if (platform_id == APQ8060)
+		baseband = BASEBAND_APQ;
+	else if (platform_id == APQ8064)
+		baseband = BASEBAND_APQ;
+	else
+		baseband = BASEBAND_MSM;
+
+	board.baseband = baseband;
+}
+
+void board_init()
+{
+	platform_detect();
+	target_detect();
+	baseband_detect();
+}
+
+unsigned board_platform_id(void)
+{
+	return board.platform;
+}
+
+unsigned board_target_id()
+{
+	return board.target;
+}
+
+unsigned board_baseband()
+{
+	return board.baseband;
+}
diff --git a/platform/msm_shared/include/board.h b/platform/msm_shared/include/board.h
new file mode 100644
index 0000000..1eef91e
--- /dev/null
+++ b/platform/msm_shared/include/board.h
@@ -0,0 +1,70 @@
+/* Copyright (c) 2012, Code Aurora Forum. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above
+ *       copyright notice, this list of conditions and the following
+ *       disclaimer in the documentation and/or other materials provided
+ *       with the distribution.
+ *     * Neither the name of Code Aurora Forum, Inc. nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#define LINUX_MACHTYPE_UNKNOWN      0
+
+/* 8960 */
+#define LINUX_MACHTYPE_8960_SIM     3230
+#define LINUX_MACHTYPE_8960_RUMI3   3231
+#define LINUX_MACHTYPE_8960_CDP     3396
+#define LINUX_MACHTYPE_8960_MTP     3397
+#define LINUX_MACHTYPE_8960_FLUID   3398
+#define LINUX_MACHTYPE_8960_APQ     3399
+#define LINUX_MACHTYPE_8960_LIQUID  3535
+
+/* 8627 */
+#define LINUX_MACHTYPE_8627_CDP     3861
+#define LINUX_MACHTYPE_8627_MTP     3862
+
+/* 8930 */
+#define LINUX_MACHTYPE_8930_CDP     3727
+#define LINUX_MACHTYPE_8930_MTP     3728
+#define LINUX_MACHTYPE_8930_FLUID   3729
+
+/* 8064 */
+#define LINUX_MACHTYPE_8064_SIM     3572
+#define LINUX_MACHTYPE_8064_RUMI3   3679
+#define LINUX_MACHTYPE_8064_CDP     3948
+#define LINUX_MACHTYPE_8064_MTP     3949
+#define LINUX_MACHTYPE_8064_LIQUID  3951
+#define LINUX_MACHTYPE_8064_HRD     3994
+#define LINUX_MACHTYPE_8064_DTV     3995
+
+struct board_data {
+	uint32_t platform;
+	uint32_t platform_hw;
+	uint32_t platform_subtype;
+	uint32_t target;
+	uint32_t baseband;
+};
+
+void board_init();
+unsigned board_platform_id();
+unsigned board_target_id();
+unsigned board_baseband();
diff --git a/platform/msm_shared/rules.mk b/platform/msm_shared/rules.mk
index a956480..91e6371 100644
--- a/platform/msm_shared/rules.mk
+++ b/platform/msm_shared/rules.mk
@@ -48,7 +48,8 @@
 			$(LOCAL_DIR)/scm.o \
 			$(LOCAL_DIR)/interrupts.o \
 			$(LOCAL_DIR)/clock-local.o \
-			$(LOCAL_DIR)/clock.o
+			$(LOCAL_DIR)/clock.o \
+			$(LOCAL_DIR)/board.o
 endif
 
 ifeq ($(PLATFORM),msm7x27a)
diff --git a/target/msm8960/init.c b/target/msm8960/init.c
index 4618550..4a5d84f 100644
--- a/target/msm8960/init.c
+++ b/target/msm8960/init.c
@@ -46,33 +46,7 @@
 #include <baseband.h>
 #include <uart_dm.h>
 #include <crypto_hash.h>
-
-/* 8960 */
-#define LINUX_MACHTYPE_8960_SIM     3230
-#define LINUX_MACHTYPE_8960_RUMI3   3231
-#define LINUX_MACHTYPE_8960_CDP     3396
-#define LINUX_MACHTYPE_8960_MTP     3397
-#define LINUX_MACHTYPE_8960_FLUID   3398
-#define LINUX_MACHTYPE_8960_APQ     3399
-#define LINUX_MACHTYPE_8960_LIQUID  3535
-
-/* 8627 */
-#define LINUX_MACHTYPE_8627_CDP     3861
-#define LINUX_MACHTYPE_8627_MTP     3862
-
-/* 8930 */
-#define LINUX_MACHTYPE_8930_CDP     3727
-#define LINUX_MACHTYPE_8930_MTP     3728
-#define LINUX_MACHTYPE_8930_FLUID   3729
-
-/* 8064 */
-#define LINUX_MACHTYPE_8064_SIM     3572
-#define LINUX_MACHTYPE_8064_RUMI3   3679
-#define LINUX_MACHTYPE_8064_CDP     3948
-#define LINUX_MACHTYPE_8064_MTP     3949
-#define LINUX_MACHTYPE_8064_LIQUID  3951
-#define LINUX_MACHTYPE_8064_HRD     3994
-#define LINUX_MACHTYPE_8064_DTV     3995
+#include <board.h>
 
 extern void dmb(void);
 extern void msm8960_keypad_init(void);
@@ -82,9 +56,6 @@
 static unsigned mmc_sdc_base[] =
     { MSM_SDC1_BASE, MSM_SDC2_BASE, MSM_SDC3_BASE, MSM_SDC4_BASE };
 
-static uint32_t platform_id;
-static uint32_t target_id;
-
 static pm8921_dev_t pmic;
 
 /* Setting this variable to different values defines the
@@ -96,13 +67,10 @@
  */
 static crypto_engine_type platform_ce_type = CRYPTO_ENGINE_TYPE_SW;
 
-static void target_detect(void);
 static void target_uart_init(void);
 
 void target_early_init(void)
 {
-	target_detect();
-
 #if WITH_DEBUG_UART
 	target_uart_init();
 #endif
@@ -124,6 +92,7 @@
 {
 	unsigned base_addr;
 	unsigned char slot;
+	unsigned platform_id = board_platform_id();
 
 	dprintf(INFO, "target_init()\n");
 
@@ -184,12 +153,7 @@
 
 unsigned board_machtype(void)
 {
-	return target_id;
-}
-
-unsigned board_platform_id(void)
-{
-	return platform_id;
+	return board_target_id();
 }
 
 crypto_engine_type board_ce_type(void)
@@ -197,139 +161,9 @@
 	return platform_ce_type;
 }
 
-void target_detect(void)
-{
-	struct smem_board_info_v6 board_info_v6;
-	unsigned int board_info_len = 0;
-	unsigned smem_status = 0;
-	unsigned format = 0;
-	unsigned id = HW_PLATFORM_UNKNOWN;
-
-	smem_status = smem_read_alloc_entry_offset(SMEM_BOARD_INFO_LOCATION,
-						   &format, sizeof(format), 0);
-	if (!smem_status) {
-		if (format == 6) {
-			board_info_len = sizeof(board_info_v6);
-
-			smem_status =
-			    smem_read_alloc_entry(SMEM_BOARD_INFO_LOCATION,
-						  &board_info_v6,
-						  board_info_len);
-			if (!smem_status) {
-				id = board_info_v6.board_info_v3.hw_platform;
-			}
-		}
-	}
-
-	platform_id = board_info_v6.board_info_v3.msm_id;
-
-	/* Detect the board we are running on */
-	if ((platform_id == MSM8960) || (platform_id == MSM8660A)
-	    || (platform_id == MSM8260A) || (platform_id == APQ8060A)) {
-		switch (id) {
-		case HW_PLATFORM_SURF:
-			target_id = LINUX_MACHTYPE_8960_CDP;
-			break;
-		case HW_PLATFORM_MTP:
-			target_id = LINUX_MACHTYPE_8960_MTP;
-			break;
-		case HW_PLATFORM_FLUID:
-			target_id = LINUX_MACHTYPE_8960_FLUID;
-			break;
-		case HW_PLATFORM_LIQUID:
-			target_id = LINUX_MACHTYPE_8960_LIQUID;
-			break;
-		default:
-			target_id = LINUX_MACHTYPE_8960_CDP;
-		}
-	} else if ((platform_id == MSM8230) || (platform_id == MSM8630)
-		   || (platform_id == MSM8930) || (platform_id == APQ8030)) {
-		switch (id) {
-		case HW_PLATFORM_SURF:
-			target_id = LINUX_MACHTYPE_8930_CDP;
-			break;
-		case HW_PLATFORM_MTP:
-			target_id = LINUX_MACHTYPE_8930_MTP;
-			break;
-		case HW_PLATFORM_FLUID:
-			target_id = LINUX_MACHTYPE_8930_FLUID;
-			break;
-		default:
-			target_id = LINUX_MACHTYPE_8930_CDP;
-		}
-	} else if ((platform_id == MSM8227) || (platform_id == MSM8627)) {
-		switch (id) {
-		case HW_PLATFORM_SURF:
-			target_id = LINUX_MACHTYPE_8627_CDP;
-			break;
-		case HW_PLATFORM_MTP:
-			target_id = LINUX_MACHTYPE_8627_MTP;
-			break;
-		default:
-			target_id = LINUX_MACHTYPE_8627_CDP;
-		}
-	} else if ((platform_id == APQ8064) || (platform_id == MPQ8064)) {
-		switch (id) {
-		case HW_PLATFORM_SURF:
-			target_id = LINUX_MACHTYPE_8064_CDP;
-			break;
-		case HW_PLATFORM_MTP:
-			target_id = LINUX_MACHTYPE_8064_MTP;
-			break;
-		case HW_PLATFORM_LIQUID:
-			target_id = LINUX_MACHTYPE_8064_LIQUID;
-			break;
-		case HW_PLATFORM_HRD:
-			target_id = LINUX_MACHTYPE_8064_HRD;
-			break;
-		case HW_PLATFORM_DTV:
-			target_id = LINUX_MACHTYPE_8064_DTV;
-			break;
-		default:
-			target_id = LINUX_MACHTYPE_8064_CDP;
-		}
-	} else {
-		dprintf(CRITICAL, "platform_id (%d) is not identified.\n",
-			platform_id);
-		ASSERT(0);
-	}
-}
-
 unsigned target_baseband()
 {
-	struct smem_board_info_v6 board_info_v6;
-	unsigned int board_info_len = 0;
-	unsigned smem_status = 0;
-	unsigned format = 0;
-	unsigned baseband = BASEBAND_MSM;
-
-	smem_status = smem_read_alloc_entry_offset(SMEM_BOARD_INFO_LOCATION,
-						   &format, sizeof(format), 0);
-	if (!smem_status) {
-		if (format >= 6) {
-			board_info_len = sizeof(board_info_v6);
-
-			smem_status =
-			    smem_read_alloc_entry(SMEM_BOARD_INFO_LOCATION,
-						  &board_info_v6,
-						  board_info_len);
-			if (!smem_status) {
-				/* Check for MDM or APQ baseband variants.  Default to MSM */
-				if (board_info_v6.platform_subtype ==
-				    HW_PLATFORM_SUBTYPE_MDM)
-					baseband = BASEBAND_MDM;
-				else if (board_info_v6.board_info_v3.msm_id ==
-					 APQ8060)
-					baseband = BASEBAND_APQ;
-				else if (board_info_v6.board_info_v3.msm_id ==
-					 APQ8064)
-					baseband = BASEBAND_APQ;
-				else
-					baseband = BASEBAND_MSM;
-			}
-		}
-	}
-	return baseband;
+	return board_baseband();
 }
 
 static unsigned target_check_power_on_reason(void)
@@ -411,6 +245,8 @@
 
 void target_uart_init(void)
 {
+	unsigned target_id = board_machtype();
+
 	switch (target_id) {
 	case LINUX_MACHTYPE_8960_SIM:
 	case LINUX_MACHTYPE_8960_RUMI3: