arm: mvebu: board/Synology: Unify legacy kernel support

Move the relevant bits from ds109.{c,h} into common/ and adjust the code
to fit both DS109 and DS414. Moreover:

* Introduce syno_board_id() which translates CONFIG_MACH_TYPE into the
  expected board ID tag value.

* Properly initialize isusbhost, mac and mtu fields from env variables.

* Set the right bootargs/bootcmd to correctly boot legacy kernel out of
  the (DS414) box. Getting the ramdisk location right is a bit tedious.

Cc: Walter Schweizer <swwa@users.sourceforge.net>
Signed-off-by: Phil Sutter <phil@nwl.cc>
Reviewed-by: Stefan Roese <sr@denx.de>
diff --git a/board/Synology/common/Makefile b/board/Synology/common/Makefile
new file mode 100644
index 0000000..62354cc
--- /dev/null
+++ b/board/Synology/common/Makefile
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright (C) 2021 Phil Sutter <phil@nwl.cc>
+
+obj-y	+= legacy.o
diff --git a/board/Synology/common/legacy.c b/board/Synology/common/legacy.c
new file mode 100644
index 0000000..3c89e92
--- /dev/null
+++ b/board/Synology/common/legacy.c
@@ -0,0 +1,76 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2021
+ * Walter Schweizer <swwa@users.sourceforge.net>
+ * Phil Sutter <phil@nwl.cc>
+ */
+
+#include <config.h>
+#include <vsprintf.h>
+#include <env.h>
+#include <net.h>
+#include <asm/setup.h>
+
+#include "legacy.h"
+
+static unsigned int syno_board_id(void)
+{
+	switch (CONFIG_MACH_TYPE) {
+	case 527:
+		return SYNO_DS109_ID;
+	case 3036:
+		return SYNO_AXP_4BAY_2BAY;
+	default:
+		return 0;
+	}
+}
+
+static unsigned int usb_port_modes(void)
+{
+	unsigned int i, ret = 0;
+	char var[32], *val;
+
+	for (i = 0; i < USBPORT_MAX; i++) {
+		snprintf(var, 32, "usb%dMode", i);
+		val = env_get(var);
+
+		if (!val || strcasecmp(val, "host"))
+			continue;
+
+		ret |= 1 << i;
+	}
+	return ret;
+}
+
+/* Support old kernels */
+void setup_board_tags(struct tag **in_params)
+{
+	struct tag_mv_uboot *t;
+	struct tag *params;
+	int i;
+
+	debug("Synology board tags...\n");
+
+	params = *in_params;
+	t = (struct tag_mv_uboot *)&params->u;
+
+	t->uboot_version = VER_NUM | syno_board_id();
+	t->tclk = CONFIG_SYS_TCLK;
+	t->sysclk = CONFIG_SYS_TCLK * 2;
+	t->isusbhost = usb_port_modes();
+
+	for (i = 0; i < ETHADDR_MAX; i++) {
+		char addrvar[16], mtuvar[16];
+
+		sprintf(addrvar, i ? "eth%daddr" : "ethaddr", i);
+		sprintf(mtuvar, i ? "eth%dmtu" : "ethmtu", i);
+
+		eth_env_get_enetaddr(addrvar, t->macaddr[i]);
+		t->mtu[i] = env_get_ulong(mtuvar, 10, 0);
+	}
+
+	params->hdr.tag = ATAG_MV_UBOOT;
+	params->hdr.size = tag_size(tag_mv_uboot);
+	params = tag_next(params);
+	*in_params = params;
+}
diff --git a/board/Synology/common/legacy.h b/board/Synology/common/legacy.h
new file mode 100644
index 0000000..0a81432
--- /dev/null
+++ b/board/Synology/common/legacy.h
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2021
+ * Walter Schweizer <swwa@users.sourceforge.net>
+ * Phil Sutter <phil@nwl.cc>
+ */
+
+#ifndef __SYNO_LEGACY_H
+#define __SYNO_LEGACY_H
+
+/* Marvell uboot parameters */
+#define ATAG_MV_UBOOT 0x41000403
+#define VER_NUM       0x03040400 /* 3.4.4 */
+
+#define BOARD_ID_BASE 0x0
+#define SYNO_DS109_ID (BOARD_ID_BASE + 0x15)
+#define SYNO_AXP_4BAY_2BAY (0xf + 1)
+
+#define ETHADDR_MAX	4
+#define USBPORT_MAX	3
+
+struct tag_mv_uboot {
+	u32 uboot_version;
+	u32 tclk;
+	u32 sysclk;
+	u32 isusbhost;
+	u8 macaddr[ETHADDR_MAX][ETH_ALEN];
+	u16 mtu[ETHADDR_MAX];
+	u32 fw_image_base;
+	u32 fw_image_size;
+};
+
+#endif /* __SYNO_LEGACY_H */
diff --git a/board/Synology/ds109/ds109.c b/board/Synology/ds109/ds109.c
index eaac954..3914faa 100644
--- a/board/Synology/ds109/ds109.c
+++ b/board/Synology/ds109/ds109.c
@@ -114,38 +114,6 @@
 		     SOFTWARE_REBOOT);
 }
 
-/* Support old kernels */
-void setup_board_tags(struct tag **in_params)
-{
-	unsigned int boardId;
-	struct tag *params;
-	struct tag_mv_uboot *t;
-	int i;
-
-	printf("Synology board tags...");
-	params = *in_params;
-	t = (struct tag_mv_uboot *)&params->u;
-
-	t->uboot_version = VER_NUM;
-
-	boardId = SYNO_DS109_ID;
-	t->uboot_version |= boardId;
-
-	t->tclk = CONFIG_SYS_TCLK;
-	t->sysclk = CONFIG_SYS_TCLK*2;
-
-	t->isusbhost = 1;
-	for (i = 0; i < 4; i++)	{
-		memset(t->macaddr[i], 0, sizeof(t->macaddr[i]));
-		t->mtu[i] = 0;
-	}
-
-	params->hdr.tag = ATAG_MV_UBOOT;
-	params->hdr.size = tag_size(tag_mv_uboot);
-	params = tag_next(params);
-	*in_params = params;
-}
-
 #ifdef CONFIG_RESET_PHY_R
 /* Configure and enable MV88E1116 PHY */
 void reset_phy(void)
diff --git a/board/Synology/ds109/ds109.h b/board/Synology/ds109/ds109.h
index cc6ef99..0cf0525 100644
--- a/board/Synology/ds109/ds109.h
+++ b/board/Synology/ds109/ds109.h
@@ -23,21 +23,4 @@
 #define MV88E1116_RGMII_TXTM_CTRL	(1 << 4)
 #define MV88E1116_RGMII_RXTM_CTRL	(1 << 5)
 
-/* Marvell uboot parameters */
-#define ATAG_MV_UBOOT 0x41000403
-#define VER_NUM       0x03040400 /* 3.4.4 */
-#define BOARD_ID_BASE 0x0
-#define SYNO_DS109_ID (BOARD_ID_BASE+0x15)
-
-struct tag_mv_uboot {
-	u32 uboot_version;
-	u32 tclk;
-	u32 sysclk;
-	u32 isusbhost;
-	char macaddr[4][6];
-	u16 mtu[4];
-	u32 fw_image_base;
-	u32 fw_image_size;
-};
-
 #endif /* __DS109_H */
diff --git a/configs/ds414_defconfig b/configs/ds414_defconfig
index 4125592..8ef2e79 100644
--- a/configs/ds414_defconfig
+++ b/configs/ds414_defconfig
@@ -20,9 +20,8 @@
 CONFIG_DEBUG_UART=y
 CONFIG_BOOTDELAY=3
 CONFIG_USE_BOOTARGS=y
-CONFIG_BOOTARGS="console=ttyS0,115200"
-CONFIG_USE_PREBOOT=y
-CONFIG_PREBOOT="usb start; sf probe"
+CONFIG_BOOTARGS="console=ttyS0,115200 ip=off initrd=0x8000040,8M root=/dev/md0 rw syno_hw_version=DS414r1 ihd_num=4 netif_num=2 flash_size=8 SataLedSpecial=1 HddHotplug=1"
+# CONFIG_USE_PREBOOT is not set
 # CONFIG_DISPLAY_BOARDINFO is not set
 CONFIG_DISPLAY_BOARDINFO_LATE=y
 CONFIG_SPL_I2C_SUPPORT=y
diff --git a/include/configs/ds109.h b/include/configs/ds109.h
index 1f033ab..35d8536 100644
--- a/include/configs/ds109.h
+++ b/include/configs/ds109.h
@@ -44,7 +44,8 @@
 	"x_bootcmd_kernel=fatload usb 0 0x6400000 uImage\0" \
 	"x_bootargs=console=ttyS0,115200\0"	\
 	"x_bootargs_root=root=/dev/sda2 rootdelay=10\0" \
-	"ipaddr=192.168.1.5\0"
+	"ipaddr=192.168.1.5\0"		\
+	"usb0Mode=host\0"
 
 /*
  * Ethernet Driver configuration
diff --git a/include/configs/ds414.h b/include/configs/ds414.h
index 8aa2d47..a2248cf 100644
--- a/include/configs/ds414.h
+++ b/include/configs/ds414.h
@@ -6,6 +6,9 @@
 #ifndef _CONFIG_SYNOLOGY_DS414_H
 #define _CONFIG_SYNOLOGY_DS414_H
 
+/* Vendor kernel expects this MACH_TYPE */
+#define CONFIG_MACH_TYPE	3036
+
 /*
  * High Level Configuration Options (easy to change)
  */
@@ -74,8 +77,18 @@
 #define CONFIG_DDR_32BIT
 
 /* Default Environment */
-#define CONFIG_BOOTCOMMAND	"sf read ${loadaddr} 0xd0000 0x700000; bootm"
 #define CONFIG_LOADADDR		0x80000
+#define CONFIG_BOOTCOMMAND					\
+	"sf probe; "						\
+	"sf read ${loadaddr} 0xd0000 0x2d0000; "		\
+	"sf read ${ramdisk_addr_r} 0x3a0000 0x430000; "		\
+	"bootm ${loadaddr} ${ramdisk_addr_r}"
+
+#define CONFIG_EXTRA_ENV_SETTINGS				\
+	"initrd_high=0xffffffff\0"				\
+	"ramdisk_addr_r=0x8000000\0"				\
+	"usb0Mode=host\0usb1Mode=host\0usb2Mode=device\0"	\
+	"ethmtu=1500\0eth1mtu=1500\0"
 
 /* increase autoneg timeout, my NIC sucks */
 #define PHY_ANEG_TIMEOUT	16000