#
# SPDX-License-Identifier: GPL-2.0
#
# Copyright (c) 2013-2023 Igor Pecovnik, igor@armbian.com
#
# This file is a part of the Armbian Build Framework
# https://github.com/armbian/build/
#
source "${BASH_SOURCE%/*}/include/rockchip64_common.inc"

BOOTSOURCE='https://github.com/radxa/u-boot.git'
BOOTBRANCH='branch:next-dev'
BOOTPATCHDIR="legacy/u-boot-radxa-rk3588"
OVERLAY_PREFIX='rockchip-rk3588'
ROCKUSB_BLOB="rk35/rk3588_spl_loader_v1.08.111.bin"

case $BRANCH in

	legacy)
		BOOTSCRIPT='boot-rk3588-legacy.cmd:boot.cmd'
		BOOTDIR='u-boot-rockchip64'
		declare -g KERNEL_MAJOR_MINOR="5.10"   # Major and minor versions of this kernel.
		declare -g -i KERNEL_GIT_CACHE_TTL=120 # 2 minutes; this is a high-traffic repo
		KERNELSOURCE='https://github.com/armbian/linux-rockchip.git'
		KERNELBRANCH='branch:rk-5.10-rkr6'
		KERNELPATCHDIR='rk35xx-legacy'
		LINUXFAMILY=rk35xx
		;;

	vendor)
		BOOTSCRIPT='boot-rk3588-legacy.cmd:boot.cmd'
		BOOTDIR='u-boot-rockchip64'
		declare -g KERNEL_MAJOR_MINOR="6.1"    # Major and minor versions of this kernel.
		declare -g -i KERNEL_GIT_CACHE_TTL=120 # 2 minutes; this is a high-traffic repo
		KERNELSOURCE='https://github.com/armbian/linux-rockchip.git'
		KERNELBRANCH='branch:rk-6.1-rkr1'
		KERNELPATCHDIR='rk35xx-vendor-6.1'
		LINUXFAMILY=rk35xx
		;;

	edge)
		LINUXFAMILY=rockchip-rk3588
		LINUXCONFIG='linux-rockchip-rk3588-'$BRANCH
		KERNEL_MAJOR_MINOR="6.8" # Major and minor versions of this kernel.
		KERNELPATCHDIR='rockchip-rk3588-edge'
		;;

	collabora)
		# Collabora's rk3588, where the action is these days
		LINUXFAMILY=rockchip-rk3588
		LINUXCONFIG='linux-rockchip-rk3588-'$BRANCH
		KERNEL_MAJOR_MINOR="6.7"                   # Major and minor versions of this kernel.
		KERNELPATCHDIR='rockchip-rk3588-collabora' # Empty/nonexisting, so zero patches, hopefully
		KERNELSOURCE='https://gitlab.collabora.com/hardware-enablement/rockchip-3588/linux.git'
		KERNELBRANCH='branch:rk3588-v6.7'
		KERNEL_DRIVERS_SKIP+=(driver_rtw88) # This is a custom kernel, while the rtw88 driver patching expects pure mainline
		;;
esac

prepare_boot_configuration

function add_host_dependencies__collabora_kernel() {
	[[ "${BRANCH}" != "collabora" ]] && return 0
	display_alert "Extra deps for collabora kernels" "bsdextrautils" "debug"
	export EXTRA_BUILD_DEPS="${EXTRA_BUILD_DEPS} bsdextrautils" # @TODO: convert to array later
}

family_tweaks_bsp() {
	:
}

function post_config_uboot_target__downgrade_gcc_errors_to_warnings() {
	declare -i gcc_major_version=0
	gcc_major_version=$(gcc -dumpversion | cut -d. -f1)
	display_alert "$BOARD" "gcc major version: ${gcc_major_version}" "debug"

	# rockchip/radxa u-boot wont' build with gcc 13 due to enum-int-mismatch
	if [[ ${gcc_major_version} -ge 13 ]]; then
		display_alert "$BOARD" "Extra CFLAGS for vendor u-boot building with gcc 13+" "info"
		uboot_cflags_array+=("-Wno-error=enum-int-mismatch")
	fi

	# orangepi u-boot wont' build with gcc 12 due to 'the comparison will always evaluate as ‘true’ for the address of ‘clk’ will never be NULL [-Werror=address]'
	if [[ ${gcc_major_version} -ge 12 ]]; then
		display_alert "$BOARD" "Extra CFLAGS for vendor u-boot building with gcc 12+" "info"
		uboot_cflags_array+=("-Wno-error=address")
	fi

	return 0
}
