#
#                             IPSTB Project
#                   ---------------------------------
#
# Copyright (C) 2009 NXP Semiconductors B.V.,
# All Rights Reserved.
#
# Filename: uboot/Makefile
#
#
# Rev Date        Author      Comments
#-------------------------------------------------------------------------------
#   1 20061102    batelaan    Initial
#   2 20061130    batelaan    Create symlink from uboot to uboot-<version> to easy
#                             using uboot include files in other packages/components,
#                             e.g. phStbBootDsp.
#   3 20061207    batelaan    Exclude from build by default, until little/big endian issue is solved.
#3.1.1 20070712   sjones      Change version number to 1.1.4
#3.1.2 20070718   sjones      Modify to actually build u-boot
#3.1.3 20070725   sjones      Update GENERATED_CONFIG: c16 -> 8335_config
#3.1.4 20070802   amccurdy    Initial changes for secure boot
#3.1.4 20070809   mwhittaker  (gbr02691) Change version number to 1.2.0
#3.1.5 20070817   amccurdy    Initial changes for secure boot (merge to 1.2.0)
#   4 20071009    mwhittaker  (gbr02691) Take GENERATED_CONFIG value from CONFIG_CONTROL_REGISTER_SETUP
#   5 20071018    sjones      Integrate ds14#3.1.4.1.4
#   6 20071121    amccurdy    Initial splashscreen support
#   7 20071125    amccurdy    Cleanup some of the splashscreen image temp files
#   8 20071128    neill       Allow splash screen to be disabled
#   9 20071129    neill       Add Splash screen display mode
#  10 20080114    amccurdy    Pass aimage signing tool selection to u-boot. Minor whitespace cleanup.
#  11 20080214    batelaan    Add dependency on prjconfig.
#  12 20080318    neill       Additional options for SCART, HDMI and Ethernet
#ds14#12 20080307 amccurdy    Indirect access to aimage signing tool via "aimage_sign" symlink
#  13 20080318    neill       Merge with ds14#12
#  14 20080325    amccurdy    Indirect access to aimage signing tool via "aimage_sign_u-boot" symlink
#                             Cleanup 'tr' syntax in u-boot_CONFIGNAME script
#  15 20080328    amccurdy    Move common Make options into u-boot_DEFMAKEOPTS, sign rescue images
#  16 20080620    neill       Add 576p and 480p splash screen modes
#  17 20080708    batelaan    Merge in NAND_SUPPORTS_READY_SIGNAL change
#  18 20080708    amccurdy    Use $(PRODUCTVERSION) as u-boot EXTRAVERSION
#  19 20080814    batelaan    Build rescue image in separate dir so not interfering with normal image build.
#                             Remove u-boot.trimsrc target.
#  22 20090107    batelaan    aimage_sign program is optional.
#  23 20090126    steel       add smsc911x support
#  24 20090305    batelaan    smsc911x takes precedence over ip3902
#  25 20090507    cnh01944    Add UDA3000 Scart switch support
#  26 20090526    amccurdy    Detect splashscreen image dimensions at compile time
#                 caroline    Add 720p splash screen support
#  27 20090701    amccurdy    Add SUNPIPE_OC2WAY support
#  28 20090924    myers       Add support for option forcing 100M full duplex
#  xx 20100212    amccurdy    Add support for configuring CPU clock speed via prjconfig
#  xx 20100303    amccurdy    Update CONFIG_UBOOT_720P_SPLASH_MODE -> CONFIG_UBOOT_720P60_SPLASH_MODE
#-------------------------------------------------------------------------------
# File: u-boot/Makefile
# Makefile to control building of u-boot.

include $(PRJROOT)/etc/first.mk

#-------------------------------------------------------------------------------
$(eval $(PKG_START))

PKG = u-boot

# Define: u-boot_VERSION
# Version of u-boot.
u-boot_VERSION = 1.2.0

$(eval $(PKG_SETUP_VARS))

#-------------------------------------------------------------------------------
# Define: u-boot_TARGET
# Name of u-boot configuration, e.g. STB220_config, STB221_config, STB225_config,
#
u-boot_TARGET = STB225v1_config

u-boot_EXTRAVERSION = .$(patsubst "%",%,$(CONFIG_NAME))$(if $(PRODUCTVERSION),-$(strip $(PRODUCTVERSION)))

u-boot_AIMAGE_SIGN = $(wildcard $(PRJROOT)/bin/aimage_sign_u-boot)

u-boot_CONFIG_NAME = $(patsubst "%",%,$(filter-out "",$(CONFIG_NAME)))
u-boot_CPU_CLK_MHZ = $(patsubst "%",%,$(filter-out "",$(CONFIG_CPU_CLK_MHZ_UBOOT)))
u-boot_SPLASH_ENABLED = $(filter-out "",$(CONFIG_UBOOT_SPLASH_FILE))
u-boot_SPLASH_NTSC = $(filter-out "",$(CONFIG_UBOOT_NTSC_SPLASH_MODE))
u-boot_SPLASH_PAL = $(filter-out "",$(CONFIG_UBOOT_PAL_SPLASH_MODE))
u-boot_SPLASH_480P = $(filter-out "",$(CONFIG_UBOOT_480P_SPLASH_MODE))
u-boot_SPLASH_576P = $(filter-out "",$(CONFIG_UBOOT_576P_SPLASH_MODE))
u-boot_SPLASH_720P = $(filter-out "",$(CONFIG_UBOOT_720P60_SPLASH_MODE))
u-boot_SPLASH_SCART = $(filter-out "",$(CONFIG_SCART_SWITCH_PRESENT))
u-boot_SPLASH_SCART_UDA3000 = $(filter-out "", $(CONFIG_SCART_UDA3000))
u-boot_SPLASH_SCART_CONNECTED_ON_I2C0 = $(filter-out "",$(CONFIG_SCART_SWITCH_CONNECTED_ON_I2C0))
u-boot_SPLASH_SCART_ENCODER_CVBS = $(filter-out "",$(CONFIG_SCART_SWITCH_DEFAULT_ENCODER_CVBS))
u-boot_SPLASH_HDMI = $(filter-out "",$(CONFIG_HDMI_PRESENT))
u-boot_ETHERNET_PRESENT = $(filter-out "",$(CONFIG_ETHERNET_PRESENT))
u-boot_ETHERNET_SMSC911X = $(filter-out "",$(CONFIG_ETHERNET_SMSC911X))
# A board with both SMSC911X and IP3902 is a special case,
# and we don't enable IP3902 in that case.
ifndef CONFIG_ETHERNET_SMSC911X
  u-boot_ETHERNET_IP3902 = $(filter-out "",$(CONFIG_ETHERNET_IP3902))
endif
u-boot_ETHERNET_OSCILLATOR = $(filter-out "",$(CONFIG_ETHERNET_CRYSTAL_PRESENT))
u-boot_ETHERNET_NETPHY_RESET_PIO_29 = $(filter-out "",$(CONFIG_NETPHY_RESET_PIO_29))
u-boot_ETHERNET_FORCE_100M_FULL_DUPLEX = $(filter-out "",$(CONFIG_ETHERNET_FORCE_100M_FULL_DUPLEX))
u-boot_NAND_SUPPORTS_READY_SIGNAL = $(filter-out "",$(CONFIG_NAND_FLASH_SUPPORTS_READY_SIGNAL))
u-boot_ABOOT_TARGET = $(patsubst "%",%,$(filter-out "",$(CONFIG_NANDBOOT_ABOOT_TARGET)))

#-------------------------------------------------------------------------------
u-boot_PATCHOPTS = --multipatch

# Define: u-boot_DEFMAKEOPTS
# Make options for building u-boot:-
# - aimage signing tool location
# - NAND support, or not
# - Don't output 25 MHz clock for ethernet phy, or not
# - u-boot boot target type
u-boot_DEFMAKEOPTS = 
u-boot_DEFMAKEOPTS += AIMAGE_SIGN="$(u-boot_AIMAGE_SIGN)"
u-boot_DEFMAKEOPTS += EXTRAVERSION="$(u-boot_EXTRAVERSION)"
u-boot_DEFMAKEOPTS += $(if $(u-boot_CPU_CLK_MHZ), CPU_CLK_MHZ=$(u-boot_CPU_CLK_MHZ))
u-boot_DEFMAKEOPTS += $(if $(u-boot_NAND_SUPPORTS_READY_SIGNAL), NAND_SUPPORTS_READY_SIGNAL=1)
u-boot_DEFMAKEOPTS += $(if $(u-boot_ETHERNET_PRESENT), ETHERNET_PRESENT=1)
u-boot_DEFMAKEOPTS += $(if $(u-boot_ETHERNET_IP3902), ETHERNET_IP3902=1)
u-boot_DEFMAKEOPTS += $(if $(u-boot_ETHERNET_SMSC911X), ETHERNET_SMSC911X=1)
u-boot_DEFMAKEOPTS += $(if $(u-boot_ETHERNET_OSCILLATOR), ETHERNET_OSCILLATOR=1)
u-boot_DEFMAKEOPTS += $(if $(u-boot_ETHERNET_NETPHY_RESET_PIO_29), ETHERNET_NETPHY_RESET_PIO_29=1)
u-boot_DEFMAKEOPTS += $(if $(u-boot_ETHERNET_FORCE_100M_FULL_DUPLEX), ETHERNET_FORCE_100M_FULL_DUPLEX=1)
u-boot_DEFMAKEOPTS += BOOT_TARGET_$(strip \
			$(if $(filter $(u-boot_ABOOT_TARGET),griffin),GRIFFIN, \
			$(if $(filter $(u-boot_ABOOT_TARGET),monaco),MONACO, \
			$(if $(filter $(u-boot_ABOOT_TARGET),mojito),MOJITO, \
			$(if $(filter $(u-boot_ABOOT_TARGET),pandora3),PANDORA3, \
			$(if $(filter $(u-boot_ABOOT_TARGET),apollo),APOLLO, \
			$(if $(filter $(u-boot_ABOOT_TARGET),bpollo),BPOLLO, \
			$(if $(filter $(u-boot_ABOOT_TARGET),cindykate),CINDYKATE, \
			$(if $(CONFIG_NAND_FLASH),GENERIC_NAND,GENERIC_NOR)))))))))=1

# Fixme: Should be controlled directly from a dedicated prjconfig option rather
#        than magically derived from prjconfig CONFIG_NAME...
#
u-boot_DEFMAKEOPTS += $(if $(filter $(u-boot_CONFIG_NAME),oc2way), SUNPIPE_OC2WAY=1)

#-------------------------------------------------------------------------------
# Define: u-boot_MAKEOPTS
# Make options for building u-boot for non-rescue builds:-
# 
u-boot_MAKEOPTS += $(u-boot_DEFMAKEOPTS)
u-boot_MAKEOPTS += $(if $(u-boot_SPLASH_ENABLED), SPLASH=1)
u-boot_MAKEOPTS += $(if $(u-boot_SPLASH_NTSC), MODE_NTSC=1)
u-boot_MAKEOPTS += $(if $(u-boot_SPLASH_PAL), MODE_PAL=1)
u-boot_MAKEOPTS += $(if $(u-boot_SPLASH_480P), MODE_480P=1)
u-boot_MAKEOPTS += $(if $(u-boot_SPLASH_576P), MODE_576P=1)
u-boot_MAKEOPTS += $(if $(u-boot_SPLASH_720P), MODE_720P=1)
u-boot_MAKEOPTS += $(if $(u-boot_SPLASH_SCART), SCART_OUTPUT=1)
u-boot_MAKEOPTS += $(if $(u-boot_SPLASH_SCART_CONNECTED_ON_I2C0), SCART_CONNECTED_ON_I2C0=1)
u-boot_MAKEOPTS += $(if $(u-boot_SPLASH_SCART_UDA3000), SCART_UDA3000=1)
u-boot_MAKEOPTS += $(if $(u-boot_SPLASH_SCART_ENCODER_CVBS), SCART_ENCODER_CVBS=1)
u-boot_MAKEOPTS += $(if $(u-boot_SPLASH_HDMI), HDMI_OUTPUT=1)

u-boot_rescue_MAKEOPTS += $(u-boot_DEFMAKEOPTS)
u-boot_rescue_MAKEOPTS += RESCUE=1

u-boot_GC += GENERATED_CONFIG=$(if $(CONFIG_NAND_FLASH),c00,$(if $(CONFIG_CONTROL_REGISTER_SETUP),$(CONFIG_CONTROL_REGISTER_SETUP),8335_config))
u-boot_rescue_GC += GENERATED_CONFIG=c00

u-boot_SPLASH_FILE = $(u-boot_SRCDIR)/splash_screens/$(if $(u-boot_SPLASH_ENABLED),$(patsubst "%",%,$(CONFIG_UBOOT_SPLASH_FILE)),nxp_stb225.bmp)

#-------------------------------------------------------------------------------
# Define: u-boot.untar_EXTRACMDS
# Deletes unneeded directories:-
# - for architectures != <ARCH>
# - boards != stb22x
define u-boot.untar_EXTRACMDS
	$(Q)cd $(u-boot_BUILDDIR) && rm -rf *arm* *avr32* *blackfin* *i386* *m68k* *microblaze* *nios* *ppc*
	$(Q)cd $(u-boot_BUILDDIR) && rm -rf `ls -d cpu/* | egrep -v 'pnx|$(ARCH)'`
	$(Q)cd $(u-boot_BUILDDIR) && rm -rf `ls -d board/* | grep -v stb22x`
	$(Q)cd $(u-boot_BUILDDIR) && rm -rf `ls -d lib* | egrep -v '$(ARCH)|generic'`
	$(Q)cd $(u-boot_BUILDDIR) && rm -rf doc/README.sbc8560 include/configs/SBC8560.h
	$(Q)mkdir -p $(u-boot_BUILDDIR)/board/stb22x
endef

$(call PKG_DEFINE_TARGETS,help clean untar)

#-------------------------------------------------------------------------------

$(call PKG_DEFINE_TARGETS,patch)

# File to indicate that u-boot.patch target has finished. It prevents u-boot.patch
# from being run again. This is needed since we move all the files after patching
# to create regular and rescue build dirs.
U-BOOT_patch_guard=$(call TSFILE,u-boot.patch@guard)

define u-boot.patchguard_CMDS
	$(Q)if [ -f $(U-BOOT_patch_guard) ]; then \
		echo ''; \
		echo '**ERROR: u-boot.patch is out-of-date or directly invoked, but we cannot re-patch'; \
		echo '(since file "$(U-BOOT_patch_guard)" exists).'; \
		echo 'I cannot handle re-patching u-boot automatically! :-('; \
		echo 'Please run u-boot.clean first. IF you know what you'; \
		echo 'are doing (!), and you know re-patching can be skipped,'; \
		echo 'then use this workaround:'; \
		echo '    touch $(call TSFILE,u-boot.patch)'; \
		echo 'and possibly:'; \
		echo '    touch $(call TSFILE,u-boot.config)'; \
		exit 1; \
	fi
endef

define u-boot.setup2builddirs_CMDS
	$(Q)mkdir $(u-boot_BUILDDIR)/.regular
	$(Q)cd $(u-boot_BUILDDIR) && mv * .regular
	$(Q)cd $(u-boot_BUILDDIR) && mv .regular regular && cp -ra regular rescue
endef

u-boot.patch_CMDS_orig := $(u-boot.patch_CMDS)
define u-boot.patch_CMDS
	$(u-boot.patchguard_CMDS)
	$(u-boot.patch_CMDS_orig)
	$(u-boot.setup2builddirs_CMDS)
	$(Q)touch $(U-BOOT_patch_guard)
endef

#-------------------------------------------------------------------------------
# Define: u-boot.config_SPLASH
# Splashscreen-specific <u-boot.config> commands.
# Creates <u-boot_BUILDDIR>/stb225_splash_data.c or removes it,
# depending on <CONFIG_UBOOT_SPLASH_FILE>.
ifneq "$(u-boot_SPLASH_ENABLED)" ""
  define u-boot.config_SPLASH_subdir
	$(Q)cd $(u-boot_BUILDDIR)/$(1)/board/stb22x/ && \
		identify >/dev/null || identify -version >/dev/null || { echo -e "\n\nError: 'identify' command (part of imagemagick) not found. Aborting.\n\n"; exit 1; } && \
		set -- $$(identify $(u-boot_SPLASH_FILE)) && \
		FILENAME=$$1 && DIMS=$$3 && WIDTH=$${DIMS%x*} && HEIGHT=$${DIMS#*x} && \
		[ "$${WIDTH}" -gt 0 -a "$${HEIGHT}" -gt 0 ] || { echo -e "\n\nError: Unable to parse output of 'identify' command. Aborting.\n\n"; exit 1; } && \
		{ echo && \
		  echo "/* $${FILENAME##*/} */" && \
		  echo && \
		  echo "#define SPLASH_IMAGE_WIDTH $${WIDTH}" && \
		  echo "#define SPLASH_IMAGE_HEIGHT $${HEIGHT}" && \
		  echo && \
		  echo "static const unsigned char stb225_splash_data_gz[$$(gzip -c9 < $(u-boot_SPLASH_FILE) | wc -c)] =" && \
		  echo "{" && \
		  gzip -c9 < $(u-boot_SPLASH_FILE) | xxd -i && \
		  echo "};" && \
		  echo; \
		} > stb225_splash_data.c
  endef
  $(call OBJ_TARGET_OTHER_DEPS, u-boot.config, $(u-boot_SPLASH_FILE))
else
  define u-boot.config_SPLASH_subdir
	$(Q)cd $(u-boot_BUILDDIR)/$(1)/board/stb22x/ && rm -f stb225_splash_data.gz stb225_splash_data.c
  endef
endif

#-------------------------------------------------------------------------------
# Target: u-boot.config
# Configures u-boot:
# - First makes mrproper.
# - Calls <u-boot.config_SPLASH> commands.
# - Finally makes <u-boot_TARGET>.
define u-boot.config_CMDS
	$(call u-boot.config_CMDS_subdir,regular)
	$(call u-boot.config_CMDS_subdir,rescue)
endef
define u-boot.config_CMDS_subdir
	cd $(u-boot_BUILDDIR)/$(1) && $(MAKECMD) mrproper
	$(call u-boot.config_SPLASH_subdir,$(1))
	cd $(u-boot_BUILDDIR)/$(1) && $(MAKECMD) $(u-boot_TARGET)
endef
$(call PKG_TARGET, u-boot.config, u-boot.$(config_PREDECESSOR))
$(call OBJ_TARGET_OTHER_DEPS, u-boot.config, $(BUILDROOT)/.prjconfig)

#-------------------------------------------------------------------------------
# Target: u-boot.make
# Makes u-boot.bin and u-boot.{nand|nor}.rescue.bin files.
define u-boot.make_CMDS
	if [ $(PRJROOT)/etc/setup_productversion.mk -nt $(u-boot_BUILDDIR)/regular/include/version_autogenerated.h ]; then \
		rm -f $(u-boot_BUILDDIR)/regular/include/version_autogenerated.h; \
	fi
	cd $(u-boot_BUILDDIR)/regular && $(MAKECMD) $(u-boot_MAKEOPTS) $(u-boot_GC) all
	cd $(u-boot_BUILDDIR) && ln -sf regular/u-boot.bin u-boot.bin
endef
$(call PKG_TARGET, u-boot.make, u-boot.$(make_PREDECESSOR))
$(call OBJ_TARGET_OTHER_DEPS, u-boot.make, $(BUILDROOT)/.prjconfig)

# We need to force a rebuild if the version number changes
$(call OBJ_TARGET_OTHER_DEPS, u-boot.make, $(PRJROOT)/etc/setup_productversion.mk)

#-------------------------------------------------------------------------------
# Target: u-boot.rescue
# Makes rescue u-boot image.
define u-boot.rescue_CMDS
	cd $(u-boot_BUILDDIR)/rescue  && $(MAKECMD) $(u-boot_rescue_MAKEOPTS) $(u-boot_rescue_GC) all
	cd $(u-boot_BUILDDIR) && ln -sf rescue/u-boot.rescue.bin u-boot.rescue.bin
endef
$(call PKG_TARGET, u-boot.rescue, u-boot.$(make_PREDECESSOR))

#-------------------------------------------------------------------------------
# Target: u-boot.install
# Synonym for <u-boot.make>
u-boot.install: u-boot.make


$(eval $(PKG_END))

include $(PRJROOT)/etc/last.mk
