[new uImage] Cleanup do_botm_linux() boot allocations

This patch moves common pre-boot allocation steps shared between PPC
and M68K to a helper routines:

common:
- get_boot_sp_limit()
- get_boot_cmline()
- get_boot_kbd()

platform:
- set_clocks_in_mhz()

Signed-off-by: Marian Balakowicz <m8@semihalf.com>
diff --git a/common/image.c b/common/image.c
index 56d6b52..39e5f23 100644
--- a/common/image.c
+++ b/common/image.c
@@ -42,9 +42,15 @@
 #endif
 
 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
+
+#ifdef CONFIG_CMD_BDI
+extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
+#endif
+
+DECLARE_GLOBAL_DATA_PTR;
 #else
 #include "mkimage.h"
-#endif
+#endif /* USE_HOSTCC*/
 
 #include <image.h>
 
@@ -501,17 +507,19 @@
  * variable and if requested ramdisk data is moved to a specified location.
  *
  * returns:
- *     initrd_start and initrd_end are set to final (after relocation) ramdisk
+ *     - initrd_start and initrd_end are set to final (after relocation) ramdisk
  *     start/end addresses if ramdisk image start and len were provided
- *     otherwise set initrd_start and initrd_end to zeros
- *
+ *     otherwise set initrd_start and initrd_end set to zeros
+ *     - returns new allc_current, next free address below BOOTMAPSZ
  */
-void ramdisk_high (ulong rd_data, ulong rd_len, bd_t *kbd, ulong sp_limit,
-		ulong sp, ulong *initrd_start, ulong *initrd_end)
+ulong ramdisk_high (ulong alloc_current, ulong rd_data, ulong rd_len,
+		bd_t *kbd, ulong sp_limit, ulong sp,
+		ulong *initrd_start, ulong *initrd_end)
 {
 	char	*s;
 	ulong	initrd_high;
 	int	initrd_copy_to_ram = 1;
+	ulong	new_alloc_current = alloc_current;
 
 	if ((s = getenv ("initrd_high")) != NULL) {
 		/* a value of "no" or a similar string will act like 0,
@@ -540,7 +548,8 @@
 			*initrd_start = rd_data;
 			*initrd_end = rd_data + rd_len;
 		} else {
-			*initrd_start  = (ulong)kbd - rd_len;
+			new_alloc_current = alloc_current - rd_len;
+			*initrd_start  = new_alloc_current;
 			*initrd_start &= ~(4096 - 1);	/* align on page */
 
 			if (initrd_high) {
@@ -566,8 +575,10 @@
 				nsp -= rd_len;
 				nsp &= ~(4096 - 1);	/* align on page */
 
-				if (nsp >= sp_limit)
+				if (nsp >= sp_limit) {
 					*initrd_start = nsp;
+					new_alloc_current = alloc_current;
+				}
 			}
 
 			show_boot_progress (12);
@@ -587,7 +598,96 @@
 	}
 	debug ("   ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
 			*initrd_start, *initrd_end);
+
+	return new_alloc_current;
+}
+
+/**
+ * get_boot_sp_limit - calculate stack pointer limit
+ * @sp: current stack pointer
+ *
+ * get_boot_sp_limit() takes current stack pointer adrress and calculates
+ * stack pointer limit, below which kernel boot data (cmdline, board info,
+ * etc.) will be allocated.
+ *
+ * returns:
+ *     stack pointer limit
+ */
+ulong get_boot_sp_limit(ulong sp)
+{
+	ulong sp_limit = sp;
+
+	sp_limit -= 2048;	/* just to be sure */
+
+	/* make sure sp_limit is within kernel mapped space */
+	if (sp_limit > CFG_BOOTMAPSZ)
+		sp_limit = CFG_BOOTMAPSZ;
+	sp_limit &= ~0xF;
+
+	return sp_limit;
+}
+
+/**
+ * get_boot_cmdline - allocate and initialize kernel cmdline
+ * @alloc_current: current boot allocation address (counting down
+ *      from sp_limit)
+ * @cmd_start: pointer to a ulong variable, will hold cmdline start
+ * @cmd_end: pointer to a ulong variable, will hold cmdline end
+ *
+ * get_boot_cmdline() allocates space for kernel command line below
+ * provided alloc_current address. If "bootargs" U-boot environemnt
+ * variable is present its contents is copied to allocated kernel
+ * command line.
+ *
+ * returns:
+ *     alloc_current after cmdline allocation
+ */
+ulong get_boot_cmdline (ulong alloc_current, ulong *cmd_start, ulong *cmd_end)
+{
+	char *cmdline;
+	char *s;
+
+	cmdline = (char *)((alloc_current - CFG_BARGSIZE) & ~0xF);
+
+	if ((s = getenv("bootargs")) == NULL)
+		s = "";
+
+	strcpy(cmdline, s);
+
+	*cmd_start = (ulong) & cmdline[0];
+	*cmd_end = *cmd_start + strlen(cmdline);
+
+	debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
+
+	return (ulong)cmdline;
+}
+
+/**
+ * get_boot_kbd - allocate and initialize kernel copy of board info
+ * @alloc_current: current boot allocation address (counting down
+ *      from sp_limit)
+ * @kbd: double pointer to board info data
+ *
+ * get_boot_kbd() - allocates space for kernel copy of board info data.
+ * Space is allocated below provided alloc_current address and kernel
+ * board info is initialized with the current u-boot board info data.
+ *
+ * returns:
+ *     alloc_current after kbd allocation
+ */
+ulong get_boot_kbd (ulong alloc_current, bd_t **kbd)
+{
+	*kbd = (bd_t *) (((ulong)alloc_current - sizeof(bd_t)) & ~0xF);
+	**kbd = *(gd->bd);
+
+	debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
+
+#if defined(DEBUG) && defined(CONFIG_CMD_BDI)
+	do_bdinfo(NULL, 0, 0, NULL);
+#endif
+
+	return (ulong)*kbd;
 }
 #endif /* CONFIG_PPC || CONFIG_M68K */
-#endif /* USE_HOSTCC */
 
+#endif /* USE_HOSTCC */