ARM: uniphier: support eMMC boot for PH1-LD11 and PH1-LD20

The Boot ROM on PH1-LD11/LD20 exports built-in APIs to load images
from an eMMC device.  They are useful to reduce the memory footprint
of SPL, rather than compiling the whole MMC framework.

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
diff --git a/arch/arm/mach-uniphier/boot-mode/Makefile b/arch/arm/mach-uniphier/boot-mode/Makefile
index d7fefc5..a898021 100644
--- a/arch/arm/mach-uniphier/boot-mode/Makefile
+++ b/arch/arm/mach-uniphier/boot-mode/Makefile
@@ -14,4 +14,8 @@
 obj-$(CONFIG_ARCH_UNIPHIER_LD11)	+= boot-mode-ld20.o
 obj-$(CONFIG_ARCH_UNIPHIER_LD20)	+= boot-mode-ld20.o
 
+ifdef CONFIG_SPL_BUILD
+obj-$(CONFIG_SPL_BOARD_LOAD_IMAGE)	+= spl_board.o
+else
 obj-$(CONFIG_CMD_PINMON) += cmd_pinmon.o
+endif
diff --git a/arch/arm/mach-uniphier/boot-mode/boot-mode.c b/arch/arm/mach-uniphier/boot-mode/boot-mode.c
index 4b744da..d34b9af 100644
--- a/arch/arm/mach-uniphier/boot-mode/boot-mode.c
+++ b/arch/arm/mach-uniphier/boot-mode/boot-mode.c
@@ -51,11 +51,30 @@
 
 u32 spl_boot_device(void)
 {
-	u32 ret;
+	u32 mode;
 
-	ret = spl_boot_device_raw();
+	mode = spl_boot_device_raw();
+
+	switch (uniphier_get_soc_type()) {
+#if defined(CONFIG_ARCH_UNIPHIER_PXS2) || defined(CONFIG_ARCH_UNIPHIER_LD6B)
+	case SOC_UNIPHIER_PXS2:
+	case SOC_UNIPHIER_LD6B:
+		if (mode == BOOT_DEVICE_USB)
+			mode = BOOT_DEVICE_NOR;
+		break;
+#endif
+#if defined(CONFIG_ARCH_UNIPHIER_LD11) || defined(CONFIG_ARCH_UNIPHIER_LD20)
+	case SOC_UNIPHIER_LD11:
+	case SOC_UNIPHIER_LD20:
+		if (mode == BOOT_DEVICE_MMC1 || mode == BOOT_DEVICE_USB)
+			mode = BOOT_DEVICE_BOARD;
+		break;
+#endif
+	default:
+		break;
+	}
 
-	return ret == BOOT_DEVICE_USB ? BOOT_DEVICE_NOR : ret;
+	return mode;
 }
 
 u32 spl_boot_mode(void)
diff --git a/arch/arm/mach-uniphier/boot-mode/spl_board.c b/arch/arm/mach-uniphier/boot-mode/spl_board.c
new file mode 100644
index 0000000..86292b6
--- /dev/null
+++ b/arch/arm/mach-uniphier/boot-mode/spl_board.c
@@ -0,0 +1,128 @@
+/*
+ * Copyright (C) 2016 Socionext Inc.
+ *   Author: Masahiro Yamada <yamada.masahiro@socionext.com>
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <common.h>
+#include <spl.h>
+#include <linux/io.h>
+#include <asm/processor.h>
+
+#include "../soc-info.h"
+
+void spl_board_announce_boot_device(void)
+{
+	printf("eMMC");
+}
+
+struct uniphier_romfunc_table {
+	void *mmc_send_cmd;
+	void *mmc_card_blockaddr;
+	void *mmc_switch_part;
+	void *mmc_load_image;
+};
+
+static const struct uniphier_romfunc_table uniphier_ld11_romfunc_table = {
+	.mmc_send_cmd = (void *)0x20d8,
+	.mmc_card_blockaddr = (void *)0x1b68,
+	.mmc_switch_part = (void *)0x1c38,
+	.mmc_load_image = (void *)0x2e48,
+};
+
+static const struct uniphier_romfunc_table uniphier_ld20_romfunc_table = {
+	.mmc_send_cmd = (void *)0x2130,
+	.mmc_card_blockaddr = (void *)0x1ba0,
+	.mmc_switch_part = (void *)0x1c70,
+	.mmc_load_image = (void *)0x2ef0,
+};
+
+int uniphier_rom_get_mmc_funcptr(int (**send_cmd)(u32, u32),
+				 int (**card_blockaddr)(u32),
+				 int (**switch_part)(int),
+				 int (**load_image)(u32, uintptr_t, u32))
+{
+	const struct uniphier_romfunc_table *table;
+
+	switch (uniphier_get_soc_type()) {
+	case SOC_UNIPHIER_LD11:
+		table = &uniphier_ld11_romfunc_table;
+		break;
+	case SOC_UNIPHIER_LD20:
+		table = &uniphier_ld20_romfunc_table;
+		break;
+	default:
+		printf("unsupported SoC\n");
+		return -EINVAL;
+	}
+
+	*send_cmd = table->mmc_send_cmd;
+	*card_blockaddr = table->mmc_card_blockaddr;
+	*switch_part = table->mmc_switch_part;
+	*load_image = table->mmc_load_image;
+
+	return 0;
+}
+
+int spl_board_load_image(void)
+{
+	int (*send_cmd)(u32 cmd, u32 arg);
+	int (*card_blockaddr)(u32 rca);
+	int (*switch_part)(int part);
+	int (*load_image)(u32 dev_addr, uintptr_t load_addr, u32 block_cnt);
+	u32 dev_addr = CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR;
+	const u32 rca = 0x1000; /* RCA assigned by Boot ROM */
+	int ret;
+
+	ret = uniphier_rom_get_mmc_funcptr(&send_cmd, &card_blockaddr,
+					   &switch_part, &load_image);
+	if (ret)
+		return ret;
+
+	/*
+	 * deselect card before SEND_CSD command.
+	 * Do not check the return code.  It fails, but it is OK.
+	 */
+	(*send_cmd)(0x071a0000, 0); /* CMD7 (arg=0) */
+
+	/* reset CMD Line */
+	writeb(0x6, 0x5a00022f);
+	while (readb(0x5a00022f))
+		cpu_relax();
+
+	ret = (*card_blockaddr)(rca);
+	if (ret) {
+		debug("card is block addressing\n");
+	} else {
+		debug("card is byte addressing\n");
+		dev_addr *= 512;
+	}
+
+	ret = (*send_cmd)(0x071a0000, rca << 16); /* CMD7: select card again */
+	if (ret)
+		printf("failed to select card\n");
+
+	ret = (*switch_part)(1); /* Switch to Boot Partition 1 */
+	if (ret)
+		printf("failed to switch partition\n");
+
+	ret = (*load_image)(dev_addr, CONFIG_SYS_TEXT_BASE, 1);
+	if (ret) {
+		printf("failed to load image\n");
+		return ret;
+	}
+
+	ret = spl_parse_image_header((void *)CONFIG_SYS_TEXT_BASE);
+	if (ret)
+		return ret;
+
+	ret = (*load_image)(dev_addr, spl_image.load_addr,
+			    spl_image.size / 512);
+	if (ret) {
+		printf("failed to load image\n");
+		return ret;
+	}
+
+	return 0;
+}