enable_extension "marvell-tools"
ARCH=arm64
BOOTBRANCH='branch:v2022.01'
BOOTENV_FILE='mvebu64.txt'
ATFSOURCE='https://github.com/MarvellEmbeddedProcessors/atf-marvell'
ATFDIR='arm-trusted-firmware-espressobin'
ATFBRANCH='branch:atf-v1.5-armada-18.12'
ATF_USE_GCC='> 7.2'
ATF_COMPILER='aarch64-linux-gnu-'

if [[ $BOARD = macchiatobin-doubleshot ]]; then
	export SCP_BL2=$SRC/cache/sources/marvell-binaries/mrvl_scp_bl2.img

	ATF_TARGET_MAP="DEBUG=1 USE_COHERENT_MEM=0 LOG_LEVEL=20 MV_DDR_PATH=$SRC/cache/sources/marvell-ddr SECURE=0 PLAT=a80x0_mcbin;;build/a80x0_mcbin/debug/bl31.bin"
	UBOOT_TARGET_MAP="DEVICE_TREE=armada-8040-mcbin ;;flash-image.bin"

	BOOTSCRIPT="boot-macchiatobin-doubleshot.cmd:boot.cmd"
	SERIALCON='ttyS0'
else
	ATF_TARGET_MAP="DEBUG=1 USE_COHERENT_MEM=0 LOG_LEVEL=20 SECURE=0 CLOCKSPRESET=CPU_800_DDR_800 DDR_TOPOLOGY=2 BOOTDEV=SPINOR PARTNUM=0 PLAT=a3700;;build/a3700/debug/bl31.bin"
	UBOOT_TARGET_MAP="DEVICE_TREE=armada-3720-espressobin ;;flash-image-*.bin"

	BOOTSCRIPT="boot-espressobin.cmd:boot.cmd"
	SERIALCON='ttyMV0'
fi

case $BRANCH in

	legacy)

		KERNELSOURCE='https://github.com/MarvellEmbeddedProcessors/linux-marvell.git'
		KERNELBRANCH='branch:linux-4.14.22-armada-18.06'
		KERNELDIR='linux-marvell'

	;;

	current)

		KERNELBRANCH='branch:linux-5.15.y'

	;;

        edge)

		KERNELBRANCH='branch:linux-5.16.y'
        ;;

esac

CPUMIN=200000
CPUMAX=1300000
GOVERNOR=ondemand

NM_IGNORE_DEVICES="interface-name:eth*,interface-name:wan*,interface-name:lan*,interface-name:br*"

write_uboot_platform()
{
	if [[ $BOARD = macchiatobin-doubleshot ]]; then
		dd if=$1/flash-image.bin of=$2 bs=512 seek=1 status=noxfer > /dev/null 2>&1
	else
		# flashing process via u-boot only for now https://www.armbian.com/espressobin/
		/bin/true
	fi
}

family_tweaks()
{
	chroot $SDCARD /bin/bash -c "apt-get -y -qq remove --auto-remove linux-sound-base alsa-base alsa-utils bluez>/dev/null 2>&1"
	[[ -f $SDCARD/etc/netplan/armbian-default.yaml ]] && sed -i "s/^  renderer.*/  renderer: networkd/" $SDCARD/etc/netplan/armbian-default.yaml
	cp $SRC/packages/bsp/mvebu64/10*  $SDCARD/etc/systemd/network/
	echo "#Marvell Espressobin Console" >> $SDCARD/etc/securetty
	echo "ttyMV0" >> $SDCARD/etc/securetty
}

atf_custom_postprocess()
{
	# prepare compilers for postprocess
	ubootdir="$SRC/cache/sources/$BOOTDIR/${BOOTBRANCH##*:}"
	export ATF1=$toolchain/$ATF_COMPILER
	export ATF2=$(find_toolchain "arm-linux-gnueabi-" "> 7.0")/arm-linux-gnueabi-
	export BL33=$ubootdir"/u-boot.bin"
	# copy extra DDR topology
	sed -i "s@^CUST | \[0-6\])@CUST | [0-7])@" $SRC/cache/sources/marvell-tools/script/buildtim.sh || true
	cp $SRC/packages/blobs/espressobin/DDR* $SRC/cache/sources/marvell-tools/tim/ddr/
}

uboot_custom_postprocess()
{
	# clean previous
	rm $ubootdir/flash-image*
	local atfdir="$SRC/cache/sources/$ATFDIR/${ATFBRANCH##*:}"
	local ubootdir="$SRC/cache/sources/$BOOTDIR/${BOOTBRANCH##*:}"
	cd $atfdir

	if [[ $BOARD = macchiatobin-doubleshot ]]; then
		make distclean >> $DEST/debug/compilation.log 2>&1

		FILENAME="flash-image.bin"
		display_alert "Building $FILENAME" "" "info"
		# http://wiki.macchiatobin.net/tiki-index.php?page=Build+from+source+-+Bootloader#Building_ATF
		make distclean >> $DEST/debug/compilation.log 2>&1
		make USE_COHERENT_MEM=0 LOG_LEVEL=20 MV_DDR_PATH=$SRC/cache/sources/marvell-ddr PLAT=a80x0_mcbin CROSS_COMPILE=$ATF1 BL33=$BL33 all fip $CTHREADS >> $DEST/debug/compilation.log 2>&1
		cp build/a80x0_mcbin/release/flash-image.bin $ubootdir/$FILENAME
	else
		clocks=( 600_600 800_800 1000_800 1200_750 )
		topology=( 512m_1cs_0 512m_2cs_0 1g_2cs_2 1g_1cs_4 2g_2cs_7 1g_1cs_5 2g_2cs_6 )
		for i in "${clocks[@]}"
		do
			for j in "${topology[@]}"
		        do
				if [[ $j = *cs_5 || $j = *cs_6 ]]; then local ddrname="DDR4"; else ddrname="DDR3"; fi
				FILENAME="flash-image-$ddrname-$j-$i.bin"
				DDR_TOPOLOGY="${j##*_}"
				CLOCKSPRESET="CPU_${i%_*}_DDR_${i##*_}"
				display_alert "Building $FILENAME" "" "info"
				make distclean >> $DEST/debug/compilation.log 2>&1
				make MV_DDR_PATH=$SRC/cache/sources/marvell-ddr CROSS_COMPILE=$ATF1 DEBUG=1 USE_COHERENT_MEM=0 LOG_LEVEL=20 SECURE=0 CLOCKSPRESET=$CLOCKSPRESET DDR_TOPOLOGY=$DDR_TOPOLOGY BOOTDEV=SPINOR PARTNUM=0 PLAT=a3700 all fip BL33=$BL33 WTP=$SRC/cache/sources/marvell-tools $CTHREADS CROSS_CM3=$ATF2 >> $DEST/debug/compilation.log 2>&1
				cp build/a3700/debug/flash-image.bin $ubootdir/$FILENAME
			done
		done
	fi

	cd $ubootdir
}
