From 536b9e94711a9d12aa96a125f096ae9e4de3d7e8 Mon Sep 17 00:00:00 2001
From: lihm <koumei.lhm@alibaba-inc.com>
Date: Sun, 18 Apr 2021 14:40:20 +0800
Subject: [PATCH 2/2] Modify u-boot using partition for ICE_C860.

---
 board/thead/ice-c860/Kconfig   |  4 ++++
 common/bootm.c                 |  1 +
 configs/ice_evb_c860_defconfig | 20 +++++++++++++++-----
 drivers/fastboot/fb_mmc.c      |  7 ++++++-
 fs/fs.c                        |  3 +++
 include/configs/ice-c860.h     | 23 +++++++++++++----------
 6 files changed, 42 insertions(+), 16 deletions(-)

diff --git a/board/thead/ice-c860/Kconfig b/board/thead/ice-c860/Kconfig
index f3c8bdf6c8..b00f2bc850 100644
--- a/board/thead/ice-c860/Kconfig
+++ b/board/thead/ice-c860/Kconfig
@@ -21,6 +21,10 @@ config SYS_TEXT_BASE
 config SPL_TEXT_BASE
     default 0xfe400000
 
+config SPL_MAX_SIZE
+    hex
+    default 0x2f000
+
 config BOARD_SPECIFIC_OPTIONS
     def_bool y
     select C860_CSKY
diff --git a/common/bootm.c b/common/bootm.c
index 902c13880d..9493ce8077 100644
--- a/common/bootm.c
+++ b/common/bootm.c
@@ -726,6 +726,7 @@ static const void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc,
 	/* check image type, for FIT images get FIT kernel node */
 	*os_data = *os_len = 0;
 	buf = map_sysmem(img_addr, 0);
+	printf("img_addr: 0x%x, buf: %d\n", img_addr, genimg_get_format(buf));
 	switch (genimg_get_format(buf)) {
 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
 	case IMAGE_FORMAT_LEGACY:
diff --git a/configs/ice_evb_c860_defconfig b/configs/ice_evb_c860_defconfig
index 5b662d64a4..326bd5e88c 100644
--- a/configs/ice_evb_c860_defconfig
+++ b/configs/ice_evb_c860_defconfig
@@ -1,11 +1,11 @@
 CONFIG_CSKY=y
 CONFIG_ENV_SIZE=0x20000
-CONFIG_ENV_OFFSET=0x10000
+CONFIG_ENV_OFFSET=0xe0000
 CONFIG_NR_DRAM_BANKS=1
 CONFIG_TARGET_ICE_C860=y
-CONFIG_SYS_PROMPT="C860 # "
+CONFIG_SYS_PROMPT="T-HEAD# "
 CONFIG_DISTRO_DEFAULTS=y
-CONFIG_FIT=y
+CONFIG_BUILD_TARGET="u-boot-with-spl.bin"
 CONFIG_DEFAULT_DEVICE_TREE="ice-c860"
 CONFIG_DISPLAY_CPUINFO=y
 CONFIG_DISPLAY_BOARDINFO=y
@@ -13,12 +13,23 @@ CONFIG_SYS_NS16550=y
 CONFIG_OF_PRIOR_STAGE=y
 # CONFIG_BLK is not set
 # CONFIG_CMD_FLASH is not set
-CONFIG_ENV_IS_NOWHERE=y
+CONFIG_CMD_ERASEENV=y
+CONFIG_CMD_GPT=y
+# CONFIG_DOS_PARTITION is not set
+# CONFIG_ISO_PARTITION is not set
+CONFIG_PARTITION_TYPE_GUID=y
+CONFIG_OF_EMBED=y
 CONFIG_ENV_IS_IN_MMC=y
+CONFIG_SYS_RELOC_GD_ENV_ADDR=y
 CONFIG_BOARD_EARLY_INIT_R
 CONFIG_BOARD_EARLY_INIT_F
 CONFIG_OF_CONTROL=y
 CONFIG_CLK=y
+CONFIG_UDP_FUNCTION_FASTBOOT=y
+CONFIG_FASTBOOT_BUF_ADDR=0x82000000
+CONFIG_FASTBOOT_FLASH=y
+CONFIG_FASTBOOT_FLASH_MMC_DEV=0
+CONFIG_FASTBOOT_CMD_OEM_FORMAT=y
 CONFIG_DM_MMC=y
 CONFIG_MMC_DW=y
 CONFIG_MMC_DW_SNPS=y
@@ -26,7 +37,6 @@ CONFIG_DM_ETH=y
 CONFIG_ETH_DESIGNWARE=y
 # CONFIG_EFI_LOADER is not set
 # CONFIG_SPL_SERIAL_SUPPORT is not set
-# CONFIG_PARTITIONS is not set
 # CONFIG_SPL_FRAMEWORK is not set
 CONFIG_SPL=y
 CONFIG_IS_ASIC=y
diff --git a/drivers/fastboot/fb_mmc.c b/drivers/fastboot/fb_mmc.c
index b0b19c5762..fa387d87f3 100644
--- a/drivers/fastboot/fb_mmc.c
+++ b/drivers/fastboot/fb_mmc.c
@@ -66,11 +66,16 @@ static lbaint_t fb_mmc_blk_write(struct blk_desc *block_dev, lbaint_t start,
 
 	for (i = 0; i < blkcnt; i += FASTBOOT_MAX_BLK_WRITE) {
 		cur_blkcnt = min((int)blkcnt - i, FASTBOOT_MAX_BLK_WRITE);
-		if (buffer) {
+		if (buffer < 0x80000000) {
 			if (fastboot_progress_callback)
 				fastboot_progress_callback("writing");
 			blks_written = blk_dwrite(block_dev, blk, cur_blkcnt,
 						  buffer + (i * block_dev->blksz));
+		} else if (buffer >= 0x80000000) {
+			if (fastboot_progress_callback)
+				fastboot_progress_callback("writing");
+			blks_written = blk_dwrite(block_dev, blk, cur_blkcnt,
+						  buffer - 0x80000000 + (i * block_dev->blksz));
 		} else {
 			if (fastboot_progress_callback)
 				fastboot_progress_callback("erasing");
diff --git a/fs/fs.c b/fs/fs.c
index 0c66d60477..5e8fb82735 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -507,12 +507,15 @@ static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
 	}
 #endif
 
+	printf("[lhm]:_fs_read\n");
 	/*
 	 * We don't actually know how many bytes are being read, since len==0
 	 * means read the whole file.
 	 */
 	buf = map_sysmem(addr, len);
+	printf("map_sysmem1 0x%x, %d, 0x%x\n", addr, len, *(int*)buf);
 	ret = info->read(filename, buf, offset, len, actread);
+	printf("map_sysmem2 0x%x, %d, 0x%x %d\n", addr, len, *(int*)buf, ret);
 	unmap_sysmem(buf);
 
 	/* If we requested a specific number of bytes, check we got it */
diff --git a/include/configs/ice-c860.h b/include/configs/ice-c860.h
index fdaf931348..5fab909003 100644
--- a/include/configs/ice-c860.h
+++ b/include/configs/ice-c860.h
@@ -94,13 +94,16 @@
 #define CONFIG_NET_RETRY_COUNT  20
 
 
-#define PHYS_FLASH_1                0x00000000 /* Flash Bank #0 */
-#define CONFIG_SYS_FLASH_BASE       PHYS_FLASH_1
+// #define PHYS_FLASH_1                0x00000000 /* Flash Bank #0 */
+// #define CONFIG_SYS_FLASH_BASE       PHYS_FLASH_1
+// #define CONFIG_SYS_MAX_FLASH_BANKS  1
+// #define CONFIG_ENV_ADDR             (CONFIG_SYS_FLASH_BASE + 0x070000)
+// #define CONFIG_SYS_MMC_ENV_DEV      0
+#define CONFIG_SYS_FLASH_BASE       0x0
 #define CONFIG_SYS_MAX_FLASH_BANKS  1
-#define CONFIG_ENV_ADDR             (CONFIG_SYS_FLASH_BASE + 0x070000)
+#define CONFIG_ENV_ADDR             (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
 #define CONFIG_SYS_MMC_ENV_DEV      0
 
-
 /* Image address in Flash */
 #define FLASH_SPL_READ_ADDR         0x100000 /* 1M */
 #define FLASH_SPL_SIZE              0x010000 /* 64K */
@@ -112,10 +115,10 @@
 
 #ifdef CONFIG_IS_ASIC
 #define TFTP_LOAD_DTB "tftpboot ${dtb_load_addr_virt} c860/ice_evb.dtb ; "
-#define TFTP_LOAD_SLAVE_DTB "tftpboot ${dtb_load_addr_virt} c810/ice_ck810_evb.dtb ; "
+// #define TFTP_LOAD_SLAVE_DTB "tftpboot ${dtb_load_addr_virt} c810/ice_ck810_evb.dtb ; "
 #else
 #define TFTP_LOAD_DTB "tftpboot ${dtb_load_addr_virt} c860/ice.dtb ; "
-#define TFTP_LOAD_SLAVE_DTB "tftpboot ${dtb_load_addr_virt} c810/ice_ck810.dtb ; "
+// #define TFTP_LOAD_SLAVE_DTB "tftpboot ${dtb_load_addr_virt} c810/ice_ck810.dtb ; "
 #endif
 
 #define CONFIG_EXTRA_ENV_SETTINGS \
@@ -125,9 +128,9 @@
     "fdt_size_sector=0x20\0" \
     "uboot_start_sector=0xa00\0"   /* uboot start sector = FLASH_UBOOT_READ_ADDR / 0x200 */ \
     "uboot_size_sector=0x400\0" \
-    "dtb_start_sector=0x21000\0"   /* dtb start sector */ \
+    "dtb_start_sector=0x1000\0"   /* dtb start sector */ \
     "dtb_size_sector=0x1000\0"    /* dtb size in sector -> 2MB */ \
-    "linux_start_sector=0x22000\0" /* linux start sector */  \
+    "linux_start_sector=0x2000\0" /* linux start sector */  \
     "linux_size_sector=0xa000\0" /* linux size in sector -> 20MB */ \
     "ramdisk_start_sector=0x2c000\0" /* ramdisk start sector */ \
     "ramdisk_size_sector=0x10000\0" /* ramdisk size in sector -> 32MB */ \
@@ -140,6 +143,7 @@
     "ramdisk_load_addr_phys=0x02000000\0" \
     "avail_addr_virt=0x80000000\0" \
     "avail_addr_phy=0x00000000\0" \
+    "set_bootargs=setenv bootargs console=ttyS0,115200 root=PARTUUID=$uuid_rootfs rootfstype=ext4 rdinit=/sbin/init rootwait rw earlyprintk clk_ignore_unused loglevel=7 eth=$ethaddr\0" \
     "update_fdt=" \
         "tftpboot ${dtb_load_addr_virt} c860/dt.dtb ; " \
         "setexpr fw_sz ${filesize} / 0x200 ; " \
@@ -177,8 +181,7 @@
 #define CONFIG_BOOTCOMMAND \
         "mmc read ${dtb_load_addr_phys} ${dtb_start_sector} ${dtb_size_sector} ; " \
         "mmc read ${linux_load_addr_phys} ${linux_start_sector} ${linux_size_sector} ; " \
-        "mmc read ${ramdisk_load_addr_phys} ${ramdisk_start_sector} ${ramdisk_size_sector} ; " \
-        "bootm ${linux_load_addr_virt} - ${dtb_load_addr_virt}"
+        "run set_bootargs; bootm ${linux_load_addr_virt} - ${dtb_load_addr_virt}"
 
 
 #endif /* __CONFIG_H */
-- 
2.17.1

