#
# (C) Copyright 2000-2006
# Wolfgang Denk, DENX Software Engineering, wd@denx.de.
#
# See file CREDITS for list of people who contributed to this
# project.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
#

VERSION			= 1
PATCHLEVEL		= 1
SUBLEVEL		= 4
EXTRAVERSION 	=
U_BOOT_VERSION	= $(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
VERSION_FILE	= include/version_autogenerated.h

HOSTARCH := $(shell uname -m | \
	sed -e s/i.86/i386/ \
	    -e s/sun4u/sparc64/ \
	    -e s/arm.*/arm/ \
	    -e s/sa110/arm/ \
	    -e s/powerpc/ppc/ \
	    -e s/macppc/ppc/)

HOSTOS := $(shell uname -s | tr '[:upper:]' '[:lower:]' | \
	    sed -e 's/\(cygwin\).*/cygwin/')

export	HOSTARCH HOSTOS

# Deal with colliding definitions from tcsh etc.
VENDOR=

#########################################################################

TOPDIR	:= $(shell if [ "$$PWD" != "" ]; then echo $$PWD; else pwd; fi)
export	TOPDIR

ifdef COMPRESSED_UBOOT
	ifndef CONFIG_SKIP_LOWLEVEL_INIT
COMPRESSED_UBOOT = 1
export export COMPRESSED_UBOOT
	endif
endif

ifeq (include/config.mk,$(wildcard include/config.mk))
# load ARCH, BOARD, and CPU configuration
include include/config.mk
export	ARCH CPU BOARD VENDOR SOC
ifndef CROSS_COMPILE
ifeq ($(HOSTARCH),ppc)
CROSS_COMPILE =
else
ifeq ($(ARCH),ppc)
CROSS_COMPILE = powerpc-linux-
endif
ifeq ($(ARCH),arm)
CROSS_COMPILE = arm-linux-
endif
ifeq ($(ARCH),i386)
ifeq ($(HOSTARCH),i386)
CROSS_COMPILE =
else
CROSS_COMPILE = i386-linux-
endif
endif
ifeq ($(ARCH),mips)
CROSS_COMPILE = mips-linux-
endif
ifeq ($(ARCH),nios)
CROSS_COMPILE = nios-elf-
endif
ifeq ($(ARCH),nios2)
CROSS_COMPILE = nios2-elf-
endif
ifeq ($(ARCH),m68k)
CROSS_COMPILE = m68k-elf-
endif
ifeq ($(ARCH),microblaze)
CROSS_COMPILE = mb-
endif
ifeq ($(ARCH),blackfin)
CROSS_COMPILE = bfin-elf-
endif
endif
endif

export	CROSS_COMPILE

# load other configuration
include $(TOPDIR)/config.mk


#########################################################################
# U-Boot objects....order is important (i.e. start must be first)
OBJS  = cpu/$(CPU)/start.o

ifdef COMPRESSED_UBOOT
	ifndef CONFIG_SKIP_LOWLEVEL_INIT
OBJS_BOOTSTRAP  = cpu/$(CPU)/start_bootstrap.o
	endif
endif

LIBS  = lib_generic/libgeneric.a
LIBS += common/libcommon.a
LIBS += board/$(BOARDDIR)/lib$(BOARD).a
LIBS += cpu/$(CPU)/lib$(CPU).a

ifdef SOC
LIBS += cpu/$(CPU)/$(SOC)/lib$(SOC).a
endif

LIBS += lib_$(ARCH)/lib$(ARCH).a
LIBS += drivers/libdrivers.a
LIBS += net/libnet.a
LIBS += rtc/librtc.a
LIBS += httpd/libhttpd.a
LIBS += $(BOARDLIBS)

ifdef COMPRESSED_UBOOT
	ifndef CONFIG_SKIP_LOWLEVEL_INIT
LIBS_BOOTSTRAP = lib_bootstrap/libbootstrap.a 
LIBS_BOOTSTRAP += board/$(BOARDDIR)/lib$(BOARD).a 
LIBS_BOOTSTRAP += cpu/$(CPU)/lib$(CPU).a
LIBS_BOOTSTRAP += cpu/$(CPU)/$(SOC)/lib$(SOC).a
	endif
endif

.PHONY : $(LIBS)

ifdef COMPRESSED_UBOOT
	ifndef CONFIG_SKIP_LOWLEVEL_INIT
.PHONY : $(LIBS_BOOTSTRAP)
	endif
endif

# Add GCC lib
PLATFORM_LIBS += -L $(shell dirname `$(CC) $(CFLAGS) -print-libgcc-file-name`) -lgcc

# The "tools" are needed early, so put this first
# Don't include stuff already done in $(LIBS)
SUBDIRS	= tools

.PHONY : $(SUBDIRS)

#########################################################################
#########################################################################

ALL = u-boot.srec u-boot.bin System.map

ifdef COMPRESSED_UBOOT
ifndef CONFIG_SKIP_LOWLEVEL_INIT
all:	$(ALL) tuboot.bin
else
all:	$(ALL) u-boot.img
endif
else
all:	$(ALL) u-boot.img
endif

u-boot.hex:	u-boot
	$(OBJCOPY) ${OBJCFLAGS} -O ihex $< $@

u-boot.srec:	u-boot
	$(OBJCOPY) ${OBJCFLAGS} -O srec $< $@

u-boot.bin:	u-boot
	$(OBJCOPY) ${OBJCFLAGS} -O binary $< $@

u-boot.img:	u-boot.bin
	@/bin/echo -e "\e[32m> Preparing U-Boot image \"u-boot.img\"...\e[0m"
	./tools/mkimage -A $(ARCH) -T firmware -C none \
		-a $(TEXT_BASE) -e 0 \
		-n $(shell sed -n -e 's/.*U_BOOT_VERSION//p' $(VERSION_FILE) | \
		sed -e 's/"[	 ]*$$/ for $(BOARD) board"/') \
		-d $< $@

u-boot.dis:	u-boot
	$(OBJDUMP) -d $< > $@

u-boot:	depend version $(SUBDIRS) $(OBJS) $(LIBS) $(LDSCRIPT)
	UNDEF_SYM=`$(OBJDUMP) -x $(LIBS) |sed  -n -e 's/.*\(__u_boot_cmd_.*\)/-u\1/p'|sort|uniq`;\
		$(LD) $(LDFLAGS) $$UNDEF_SYM $(OBJS) \
		--start-group $(LIBS) --end-group $(PLATFORM_LIBS) \
		-Map u-boot.map -o u-boot

$(LIBS):
	$(MAKE) -C `dirname $@`

$(SUBDIRS):
	$(MAKE) -C $@ all

ifdef COMPRESSED_UBOOT
ifndef CONFIG_SKIP_LOWLEVEL_INIT
LZMA = $(BUILD_TOPDIR)/host_util/lzma

tuboot.bin:	System.map bootstrap.bin u-boot.lzimg	
	@cat bootstrap.bin > $@
	@cat u-boot.lzimg >> $@

u-boot.lzimg: $(obj)u-boot.bin System.map
	@rm -rf u-boot.bin.lzma
	$(LZMA) --best --keep $(obj)u-boot.bin
	@/bin/echo -e "\e[32m> Preparing compressed U-Boot image \"u-boot.lzimg\"...\e[0m"
	./tools/mkimage -A mips -T firmware -C lzma \
		-a 0x$(shell grep "T _start" $(TOPDIR)/System.map | awk '{ printf "%s", $$1 }') \
		-e 0x$(shell grep "T _start" $(TOPDIR)/System.map | awk '{ printf "%s", $$1 }') \
		-n 'u-boot image' -d $(obj)u-boot.bin.lzma $@

bootstrap.bin:	bootstrap
	$(OBJCOPY) ${OBJCFLAGS} -O binary $< $@

bootstrap:	depend version $(SUBDIRS) $(OBJS_BOOTSTRAP) $(LIBS_BOOTSTRAP) $(LDSCRIPT_BOOTSTRAP)
	UNDEF_SYM=`$(OBJDUMP) -x $(LIBS_BOOTSTRAP) |sed  -n -e 's/.*\(__u_boot_cmd_.*\)/-u\1/p'|sort|uniq`;\
		$(LD) $(LDFLAGS_BOOTSTRAP) $$UNDEF_SYM $(OBJS_BOOTSTRAP) \
		--start-group $(LIBS_BOOTSTRAP) --end-group $(PLATFORM_LIBS) \
		-Map bootstrap.map -o bootstrap

$(LIBS_BOOTSTRAP):
	$(MAKE) -C `dirname $@`
endif
endif

version:
	@echo -n "#define U_BOOT_VERSION \"U-Boot " > $(VERSION_FILE); \
		echo -n "$(U_BOOT_VERSION)" >> $(VERSION_FILE); \
		echo "\"" >> $(VERSION_FILE)

gdbtools:
	$(MAKE) -C tools/gdb || exit 1

depend dep:
	@for dir in $(SUBDIRS) ; do $(MAKE) -C $$dir .depend ; done

tags:
	ctags -w `find $(SUBDIRS) include \
		lib_generic board/$(BOARDDIR) cpu/$(CPU) lib_$(ARCH) \
		fs/cramfs fs/fat fs/fdos fs/jffs2 \
		net disk rtc dtt drivers drivers/sk98lin common \
		\( -name CVS -prune \) -o \( -name '*.[ch]' -print \)`

etags:
	etags -a `find $(SUBDIRS) include \
		lib_generic board/$(BOARDDIR) cpu/$(CPU) lib_$(ARCH) \
		fs/cramfs fs/fat fs/fdos fs/jffs2 \
		net disk rtc dtt drivers drivers/sk98lin common \
		\( -name CVS -prune \) -o \( -name '*.[ch]' -print \)`

System.map:	u-boot
	@$(NM) $< | \
		grep -v '\(compiled\)\|\(\.o$$\)\|\( [aUw] \)\|\(\.\.ng$$\)\|\(LASH[RL]DI\)' | \
		sort > System.map

#########################################################################
else
all install u-boot u-boot.srec depend dep:
	@echo "System not configured - see README" >&2
	@ exit 1
endif

#########################################################################

unconfig:
	@rm -f include/config.h include/config.mk board/*/config.tmp

#========================================================================
# MIPS
#========================================================================
#########################################################################
## MIPS32 AR7100 (24K)
#########################################################################
common_config :
	@ >include/config.h

ifdef CONFIG_BOOTDELAY
	@echo "#define CONFIG_BOOTDELAY "$(CONFIG_BOOTDELAY)   >> include/config.h
endif

ifdef CONFIG_MAX_UBOOT_SIZE_KB
	@echo "#define CONFIG_MAX_UBOOT_SIZE_KB "$(CONFIG_MAX_UBOOT_SIZE_KB) >> include/config.h
endif

ifdef CFG_PLL_FREQ
	@echo "#define CFG_PLL_FREQ "$(CFG_PLL_FREQ)           >> include/config.h
endif

ifdef DISABLE_CONSOLE_OUTPUT
	@echo "#define CONFIG_SILENT_CONSOLE                1" >> include/config.h
endif

ifdef CONFIG_SKIP_LOWLEVEL_INIT
	@echo "#define CONFIG_SKIP_LOWLEVEL_INIT            1" >> include/config.h
	@echo "#undef COMPRESSED_UBOOT"                        >> include/config.h
endif

	@echo "#define CONFIG_DELAY_TO_AUTORUN_HTTPD        3" >> include/config.h
	@echo "#define CONFIG_DELAY_TO_AUTORUN_CONSOLE      5" >> include/config.h
	@echo "#define CONFIG_DELAY_TO_AUTORUN_NETCONSOLE   7" >> include/config.h

	# max delay time for button pressing
	@echo "#define CONFIG_MAX_BUTTON_PRESSING          10" >> include/config.h

	# don't show info about console (in, out, err...)
	@echo "#define CFG_CONSOLE_INFO_QUIET"                 >> include/config.h

hornet_common_config : common_config
	@echo "#define CONFIG_AR7240                        1" >> include/config.h
	@echo "#define CONFIG_MACH_HORNET                   1" >> include/config.h
	@echo "#define CONFIG_HORNET_1_1_WAR                1" >> include/config.h

wr703n_config : unconfig hornet_common_config
	@/bin/echo -e '\e[32m> Configuring for TP-Link TL-WR703N at:' `date` '\e[0m'
	@echo "#define CONFIG_FOR_TPLINK_WR703N_V1          1" >> include/config.h
	@echo "#define GPIO_SYS_LED_BIT                    27" >> include/config.h
	@echo "#define GPIO_SYS_LED_ON                      0" >> include/config.h
	@echo "#define GPIO_RST_BUTTON_BIT                 11" >> include/config.h
	@echo "#define DEFAULT_FLASH_SIZE_IN_MB             4" >> include/config.h
	@echo "#define BOARD_CUSTOM_STRING                  \"AP121 (AR9331) U-Boot for TL-WR703N\"" >> include/config.h
	
	@./mkconfig -a ap121 mips mips ap121 ar7240 ar7240

wr720n_v3_CH_config : unconfig hornet_common_config
	@/bin/echo -e '\e[32m> Configuring for TP-Link TL-WR720N v3 CH at:' `date` '\e[0m'
	@echo "#define CONFIG_FOR_TPLINK_WR720N_V3          1" >> include/config.h
	@echo "#define GPIO_SYS_LED_BIT                    27" >> include/config.h
	@echo "#define GPIO_SYS_LED_ON                      0" >> include/config.h
	@echo "#define GPIO_RST_BUTTON_BIT                 11" >> include/config.h
	@echo "#define DEFAULT_FLASH_SIZE_IN_MB             4" >> include/config.h
	@echo "#define BOARD_CUSTOM_STRING                  \"AP121 (AR9331) U-Boot for TL-WR720N v3 CH\"" >> include/config.h
	
	@./mkconfig -a ap121 mips mips ap121 ar7240 ar7240

wr710n_config : unconfig hornet_common_config
	@/bin/echo -e '\e[32m> Configuring for TP-Link TL-WR710N at:' `date` '\e[0m'
	@echo "#define CONFIG_FOR_TPLINK_WR710N_V1          1" >> include/config.h
	@echo "#define GPIO_SYS_LED_BIT                    27" >> include/config.h
	@echo "#define GPIO_SYS_LED_ON                      0" >> include/config.h
	@echo "#define GPIO_RST_BUTTON_BIT                 11" >> include/config.h
	@echo "#define DEFAULT_FLASH_SIZE_IN_MB             8" >> include/config.h
	@echo "#define BOARD_CUSTOM_STRING                  \"AP121 (AR9331) U-Boot for TL-WR710N\"" >> include/config.h
	
	@./mkconfig -a ap121 mips mips ap121 ar7240 ar7240

mr3020_config : unconfig hornet_common_config
	@/bin/echo -e '\e[32m> Configuring for TP-Link TL-MR3020 at:' `date`.'\e[0m'
	@echo "#define CONFIG_FOR_TPLINK_MR3020_V1          1" >> include/config.h
	@echo "#define GPIO_INTERNET_LED_BIT               27" >> include/config.h
	@echo "#define GPIO_INTERNET_LED_ON                 0" >> include/config.h
	@echo "#define GPIO_WPS_LED_BIT                    26" >> include/config.h
	@echo "#define GPIO_WPS_LED_ON                      0" >> include/config.h
	@echo "#define GPIO_WLAN_LED_BIT                    0" >> include/config.h
	@echo "#define GPIO_WLAN_LED_ON                     1" >> include/config.h
	@echo "#define GPIO_ETH_LED_BIT                    17" >> include/config.h
	@echo "#define GPIO_ETH_LED_ON                      0" >> include/config.h
	@echo "#define GPIO_RST_BUTTON_BIT                 11" >> include/config.h
	@echo "#define DEFAULT_FLASH_SIZE_IN_MB             4" >> include/config.h
	@echo "#define BOARD_CUSTOM_STRING                  \"AP121 (AR9331) U-Boot for TL-MR3020\"" >> include/config.h
	
	@./mkconfig -a ap121 mips mips ap121 ar7240 ar7240

mr3040_config : unconfig hornet_common_config
	@/bin/echo -e '\e[32m> Configuring for TP-Link TL-MR3040 at:' `date` '\e[0m'
	@echo "#define CONFIG_FOR_TPLINK_MR3040_V1V2        1" >> include/config.h
	@echo "#define GPIO_INTERNET_LED_BIT               27" >> include/config.h
	@echo "#define GPIO_INTERNET_LED_ON                 0" >> include/config.h
	@echo "#define GPIO_WLAN_LED_BIT                   26" >> include/config.h
	@echo "#define GPIO_WLAN_LED_ON                     0" >> include/config.h
	@echo "#define GPIO_ETH_LED_BIT                    17" >> include/config.h
	@echo "#define GPIO_ETH_LED_ON                      0" >> include/config.h
	@echo "#define GPIO_RST_BUTTON_BIT                 11" >> include/config.h
	@echo "#define DEFAULT_FLASH_SIZE_IN_MB             4" >> include/config.h
	@echo "#define BOARD_CUSTOM_STRING                  \"AP121 (AR9331) U-Boot for TL-MR3040\"" >> include/config.h
	
	@./mkconfig -a ap121 mips mips ap121 ar7240 ar7240

mr10u_config : unconfig hornet_common_config
	@/bin/echo -e '\e[32m> Configuring for TP-Link TL-MR10U at:' `date` '\e[0m'
	@echo "#define CONFIG_FOR_TPLINK_MR10U_V1           1" >> include/config.h
	@echo "#define GPIO_SYS_LED_BIT                    27" >> include/config.h
	@echo "#define GPIO_SYS_LED_ON                      0" >> include/config.h
	@echo "#define GPIO_RST_BUTTON_BIT                 11" >> include/config.h
	@echo "#define DEFAULT_FLASH_SIZE_IN_MB             4" >> include/config.h
	@echo "#define BOARD_CUSTOM_STRING                  \"AP121 (AR9331) U-Boot for TL-MR10U\"" >> include/config.h
	
	@./mkconfig -a ap121 mips mips ap121 ar7240 ar7240

mr13u_config : unconfig hornet_common_config
	@/bin/echo -e '\e[32m> Configuring for TP-Link TL-MR13U at:' `date` '\e[0m'
	@echo "#define CONFIG_FOR_TPLINK_MR13U_V1           1" >> include/config.h
	@echo "#define GPIO_SYS_LED_BIT                    27" >> include/config.h
	@echo "#define GPIO_SYS_LED_ON                      0" >> include/config.h
	@echo "#define GPIO_RST_BUTTON_BIT                 11" >> include/config.h
	@echo "#define DEFAULT_FLASH_SIZE_IN_MB             4" >> include/config.h
	@echo "#define BOARD_CUSTOM_STRING                  \"AP121 (AR9331) U-Boot for TL-MR13U\"" >> include/config.h
	
	@./mkconfig -a ap121 mips mips ap121 ar7240 ar7240

wr740n_v4_config : unconfig hornet_common_config
	@/bin/echo -e '\e[32m> Configuring for TP-Link TL-WR740N v4 at:' `date` '\e[0m'
	@echo "#define CONFIG_FOR_TPLINK_WR740N_V4          1" >> include/config.h
	@echo "#define GPIO_SYS_LED_BIT                    27" >> include/config.h
	@echo "#define GPIO_SYS_LED_ON                      0" >> include/config.h
	@echo "#define GPIO_WLAN_LED_BIT                    0" >> include/config.h
	@echo "#define GPIO_WLAN_LED_ON                     1" >> include/config.h
	@echo "#define GPIO_LAN1_LED_BIT                   14" >> include/config.h
	@echo "#define GPIO_LAN1_LED_ON                     1" >> include/config.h
	@echo "#define GPIO_LAN2_LED_BIT                   15" >> include/config.h
	@echo "#define GPIO_LAN2_LED_ON                     1" >> include/config.h
	@echo "#define GPIO_LAN3_LED_BIT                   16" >> include/config.h
	@echo "#define GPIO_LAN3_LED_ON                     1" >> include/config.h
	@echo "#define GPIO_LAN4_LED_BIT                   17" >> include/config.h
	@echo "#define GPIO_LAN4_LED_ON                     0" >> include/config.h
	@echo "#define GPIO_INTERNET_LED_BIT               13" >> include/config.h
	@echo "#define GPIO_INTERNET_LED_ON                 1" >> include/config.h
	@echo "#define GPIO_QSS_LED_BIT                     1" >> include/config.h
	@echo "#define GPIO_QSS_LED_ON                      1" >> include/config.h
	@echo "#define GPIO_RST_BUTTON_BIT                 11" >> include/config.h
	@echo "#define DEFAULT_FLASH_SIZE_IN_MB             4" >> include/config.h
	@echo "#define BOARD_CUSTOM_STRING                  \"AP121 (AR9331) U-Boot for TL-WR74xN/D v4\"" >> include/config.h
	
	@./mkconfig -a ap121 mips mips ap121 ar7240 ar7240

mr3220_v2_config : unconfig hornet_common_config
	@/bin/echo -e '\e[32m> Configuring for TP-Link TL-MR3220 v2 at:' `date` '\e[0m'
	@echo "#define CONFIG_FOR_TPLINK_MR3220_V2          1" >> include/config.h
	@echo "#define GPIO_SYS_LED_BIT                    27" >> include/config.h
	@echo "#define GPIO_SYS_LED_ON                      0" >> include/config.h
	@echo "#define GPIO_WLAN_LED_BIT                    0" >> include/config.h
	@echo "#define GPIO_WLAN_LED_ON                     1" >> include/config.h
	@echo "#define GPIO_LAN1_LED_BIT                   14" >> include/config.h
	@echo "#define GPIO_LAN1_LED_ON                     1" >> include/config.h
	@echo "#define GPIO_LAN2_LED_BIT                   15" >> include/config.h
	@echo "#define GPIO_LAN2_LED_ON                     1" >> include/config.h
	@echo "#define GPIO_LAN3_LED_BIT                   16" >> include/config.h
	@echo "#define GPIO_LAN3_LED_ON                     1" >> include/config.h
	@echo "#define GPIO_LAN4_LED_BIT                   17" >> include/config.h
	@echo "#define GPIO_LAN4_LED_ON                     0" >> include/config.h
	@echo "#define GPIO_INTERNET_LED_BIT               13" >> include/config.h
	@echo "#define GPIO_INTERNET_LED_ON                 1" >> include/config.h
	@echo "#define GPIO_QSS_LED_BIT                     1" >> include/config.h
	@echo "#define GPIO_QSS_LED_ON                      1" >> include/config.h
	@echo "#define GPIO_USB_LED_BIT                    26" >> include/config.h
	@echo "#define GPIO_USB_LED_ON                      1" >> include/config.h
	@echo "#define GPIO_RST_BUTTON_BIT                 11" >> include/config.h
	@echo "#define DEFAULT_FLASH_SIZE_IN_MB             4" >> include/config.h
	@echo "#define BOARD_CUSTOM_STRING                  \"AP121 (AR9331) U-Boot for TL-MR3220 v2\"" >> include/config.h
	
	@./mkconfig -a ap121 mips mips ap121 ar7240 ar7240

dir505_config : unconfig hornet_common_config
	@/bin/echo -e '\e[32m> Configuring for D-Link DIR-505 at:' `date` '\e[0m'
	@echo "#define CONFIG_FOR_DLINK_DIR505_A1           1" >> include/config.h
	@echo "#define GPIO_SYS_LED_BIT                    27" >> include/config.h
	@echo "#define GPIO_SYS_LED_ON                      0" >> include/config.h

	# we will use WPS button instead of reset
	@echo "#define GPIO_RST_BUTTON_BIT                 11" >> include/config.h
	@echo "#define GPIO_RST_BUTTON_IS_ACTIVE_LOW        1" >> include/config.h
	
	@echo "#define DEFAULT_FLASH_SIZE_IN_MB             8" >> include/config.h
	@echo "#define BOARD_CUSTOM_STRING                  \"AP121 (AR9331) U-Boot for DIR-505\"" >> include/config.h
	
	@./mkconfig -a ap121 mips mips ap121 ar7240 ar7240

gs_oolite_v1_dev_config : unconfig hornet_common_config
	@/bin/echo -e '\e[32m> Configuring for GS-Oolite v1 with dev board at:' `date` '\e[0m'
	@echo "#define CONFIG_FOR_GS_OOLITE_V1_DEV          1" >> include/config.h
	@echo "#define GPIO_SYS_LED_BIT                    27" >> include/config.h
	@echo "#define GPIO_SYS_LED_ON                      0" >> include/config.h
	@echo "#define GPIO_WAN_LED_BIT                    17" >> include/config.h
	@echo "#define GPIO_WAN_LED_ON                      0" >> include/config.h
	@echo "#define GPIO_LAN1_LED_BIT                   13" >> include/config.h
	@echo "#define GPIO_LAN1_LED_ON                     0" >> include/config.h
	@echo "#define GPIO_LAN2_LED_BIT                   15" >> include/config.h
	@echo "#define GPIO_LAN2_LED_ON                     0" >> include/config.h
	@echo "#define GPIO_RST_BUTTON_BIT                 11" >> include/config.h
	@echo "#define DEFAULT_FLASH_SIZE_IN_MB            16" >> include/config.h
	@echo "#define BOARD_CUSTOM_STRING                  \"AP121 (AR9331) U-Boot for GS-Oolite v1\"" >> include/config.h
	
	@./mkconfig -a ap121 mips mips ap121 ar7240 ar7240

carambola2_config : unconfig hornet_common_config
	@/bin/echo -e '\e[32m> Configuring for 8devices Carambola 2 at:' `date` '\e[0m'
	@echo "#define CONFIG_FOR_8DEVICES_CARAMBOLA2       1" >> include/config.h

	# Carambola 2 uses uncompressed version
	@echo "#undef COMPRESSED_UBOOT"                        >> include/config.h

	# Carambola 2 uses 40 MHz oscillator
	@echo "#define CONFIG_40MHZ_XTAL_SUPPORT            1" >> include/config.h

	@echo "#define GPIO_WLAN_LED_BIT                    0" >> include/config.h
	@echo "#define GPIO_WLAN_LED_ON                     0" >> include/config.h
	@echo "#define GPIO_RST_BUTTON_BIT                 11" >> include/config.h

	# Carambola 2 development board has RST button pulled up, so it is active at low
	@echo "#define GPIO_RST_BUTTON_IS_ACTIVE_LOW        1" >> include/config.h

	@echo "#define DEFAULT_FLASH_SIZE_IN_MB            16" >> include/config.h
	@echo "#define BOARD_CUSTOM_STRING                  \"AP121 (AR9331) U-Boot for CARAMBOLA2 v1\"" >> include/config.h
	
	@./mkconfig -a ap121 mips mips ap121 ar7240 ar7240

dragino_v2_ms14_config : unconfig hornet_common_config
	@/bin/echo -e '\e[32m> Configuring for Dragino Dragino v2 (MS14) at:' `date` '\e[0m'
	@echo "#define CONFIG_FOR_DRAGINO_V2                1" >> include/config.h
	@echo "#undef  COMPRESSED_UBOOT"                       >> include/config.h
	@echo "#define GPIO_WLAN_LED_BIT                    0" >> include/config.h
	@echo "#define GPIO_WLAN_LED_ON                     1" >> include/config.h
	@echo "#define GPIO_WAN_LED_BIT                    17" >> include/config.h
	@echo "#define GPIO_WAN_LED_ON                      0" >> include/config.h
	@echo "#define GPIO_LAN_LED_BIT                    13" >> include/config.h
	@echo "#define GPIO_LAN_LED_ON                      0" >> include/config.h
	@echo "#define GPIO_INTERNET_LED_BIT               28" >> include/config.h
	@echo "#define GPIO_INTERNET_LED_ON                 1" >> include/config.h
	@echo "#define GPIO_RST_BUTTON_BIT                 11" >> include/config.h
	@echo "#define GPIO_RST_BUTTON_IS_ACTIVE_LOW        1" >> include/config.h
	@echo "#define DEFAULT_FLASH_SIZE_IN_MB            16" >> include/config.h
	@echo "#define WEBFAILSAFE_DISABLE_ART_UPGRADE      1" >> include/config.h
	@echo "#define WEBFAILSAFE_DISABLE_UBOOT_UPGRADE    1" >> include/config.h
	@echo "#define BOARD_CUSTOM_STRING                  \"AP121 (AR9331) U-Boot for Dragino v2 MS14\"" >> include/config.h
	
	@./mkconfig -a ap121 mips mips ap121 ar7240 ar7240

villagetelco_mp2_config : unconfig hornet_common_config
	@/bin/echo -e '\e[32m> Configuring for Village Telco Mesh Potato 2 at:' `date` '\e[0m'
	@echo "#define CONFIG_FOR_MESH_POTATO_V2            1" >> include/config.h
	@echo "#undef  COMPRESSED_UBOOT"                       >> include/config.h
	@echo "#define GPIO_WLAN_LED_BIT                    0" >> include/config.h
	@echo "#define GPIO_WLAN_LED_ON                     1" >> include/config.h
	@echo "#define GPIO_WAN_LED_BIT                    17" >> include/config.h
	@echo "#define GPIO_WAN_LED_ON                      0" >> include/config.h
	@echo "#define GPIO_LAN_LED_BIT                    13" >> include/config.h
	@echo "#define GPIO_LAN_LED_ON                      0" >> include/config.h
	@echo "#define GPIO_INTERNET_LED_BIT               28" >> include/config.h
	@echo "#define GPIO_INTERNET_LED_ON                 1" >> include/config.h
	@echo "#define GPIO_RST_BUTTON_BIT                 11" >> include/config.h
	@echo "#define GPIO_RST_BUTTON_IS_ACTIVE_LOW        1" >> include/config.h
	@echo "#define DEFAULT_FLASH_SIZE_IN_MB            16" >> include/config.h
	@echo "#define WEBFAILSAFE_DISABLE_ART_UPGRADE      1" >> include/config.h
	@echo "#define WEBFAILSAFE_DISABLE_UBOOT_UPGRADE    1" >> include/config.h
	@echo "#define BOARD_CUSTOM_STRING                  \"AP121 (AR9331) U-Boot for Village Telco Mesh Potato 2\"" >> include/config.h
	
	@./mkconfig -a ap121 mips mips ap121 ar7240 ar7240

gl-inet_config : unconfig hornet_common_config
	@/bin/echo -e '\e[32m> Configuring for GL.iNet at:' `date` '\e[0m'
	@echo "#define CONFIG_FOR_GL_INET                   1" >> include/config.h
	@echo "#define GPIO_WLAN_LED_BIT                    0" >> include/config.h
	@echo "#define GPIO_WLAN_LED_ON                     1" >> include/config.h
	@echo "#define GPIO_LAN_LED_BIT                    13" >> include/config.h
	@echo "#define GPIO_LAN_LED_ON                      1" >> include/config.h
	@echo "#define GPIO_RST_BUTTON_BIT                 11" >> include/config.h
	@echo "#define DEFAULT_FLASH_SIZE_IN_MB             8" >> include/config.h
	@echo "#define BOARD_CUSTOM_STRING                  \"AP121 (AR9331) U-Boot for GL.iNet\"" >> include/config.h

	@./mkconfig -a ap121 mips mips ap121 ar7240 ar7240

wasp_common_config : common_config
	@echo "#define CONFIG_AR7240                        1" >> include/config.h
	@echo "#define CONFIG_WASP                          1" >> include/config.h
	@echo "#define CONFIG_WASP_SUPPORT                  1" >> include/config.h

wdr3600_43x0_config : unconfig wasp_common_config
	@/bin/echo -e '\e[32m> Configuring for TP-Link TL-WDR3600/43x0 at:' `date` '\e[0m'
	@echo "#define CONFIG_FOR_TPLINK_WDR3600_WDR43X0_V1 1" >> include/config.h
	@echo "#define DDR2_32BIT_SUPPORT                   1" >> include/config.h
	@echo "#define CFG_ATHRS17_PHY                      1" >> include/config.h
	@echo "#define CFG_AG7240_NMACS                     1" >> include/config.h
	@echo "#define CFG_DUAL_PHY_SUPPORT                 1" >> include/config.h
	@echo "#define GPIO_SYS_LED_BIT                    14" >> include/config.h
	@echo "#define GPIO_SYS_LED_ON                      0" >> include/config.h
	@echo "#define GPIO_WLAN_2G_LED_BIT                13" >> include/config.h
	@echo "#define GPIO_WLAN_2G_LED_ON                  0" >> include/config.h
	@echo "#define GPIO_USB1_LED_BIT                   11" >> include/config.h
	@echo "#define GPIO_USB1_LED_ON                     0" >> include/config.h
	@echo "#define GPIO_USB2_LED_BIT                   12" >> include/config.h
	@echo "#define GPIO_USB2_LED_ON                     0" >> include/config.h
	@echo "#define GPIO_QSS_LED_BIT                    15" >> include/config.h
	@echo "#define GPIO_QSS_LED_ON                      0" >> include/config.h
	@echo "#define GPIO_RST_BUTTON_BIT                 16" >> include/config.h
	@echo "#define GPIO_RST_BUTTON_IS_ACTIVE_LOW        1" >> include/config.h
	@echo "#define DEFAULT_FLASH_SIZE_IN_MB             8" >> include/config.h
	@echo "#define BOARD_CUSTOM_STRING                  \"DB120 (AR9344) U-Boot for TL-WDR3600/43x0\"" >> include/config.h

	@./mkconfig -a db12x mips mips db12x ar7240 ar7240

wdr3500_config : unconfig wasp_common_config
	@/bin/echo -e '\e[32m> Configuring for TP-Link TL-WDR3500 at:' `date` '\e[0m'
	@echo "#define CONFIG_FOR_TPLINK_WDR3500_V1         1" >> include/config.h
	@echo "#define DDR2_32BIT_SUPPORT                   1" >> include/config.h
	@echo "#define CFG_ATHRS27_PHY                      1" >> include/config.h
	@echo "#define CFG_AG7240_NMACS                     2" >> include/config.h
	@echo "#define GPIO_SYS_LED_BIT                    14" >> include/config.h
	@echo "#define GPIO_SYS_LED_ON                      0" >> include/config.h
	@echo "#define GPIO_WLAN_2G_LED_BIT                13" >> include/config.h
	@echo "#define GPIO_WLAN_2G_LED_ON                  0" >> include/config.h
	@echo "#define GPIO_LAN1_LED_BIT                   19" >> include/config.h
	@echo "#define GPIO_LAN1_LED_ON                     0" >> include/config.h
	@echo "#define GPIO_LAN2_LED_BIT                   20" >> include/config.h
	@echo "#define GPIO_LAN2_LED_ON                     0" >> include/config.h
	@echo "#define GPIO_LAN3_LED_BIT                   21" >> include/config.h
	@echo "#define GPIO_LAN3_LED_ON                     0" >> include/config.h
	@echo "#define GPIO_LAN4_LED_BIT                   22" >> include/config.h
	@echo "#define GPIO_LAN4_LED_ON                     0" >> include/config.h
	@echo "#define GPIO_INTERNET_LED_BIT               18" >> include/config.h
	@echo "#define GPIO_INTERNET_LED_ON                 0" >> include/config.h
	@echo "#define GPIO_QSS_LED_BIT                    15" >> include/config.h
	@echo "#define GPIO_QSS_LED_ON                      0" >> include/config.h
	@echo "#define GPIO_USB_LED_BIT                    11" >> include/config.h
	@echo "#define GPIO_USB_LED_ON                      0" >> include/config.h
	@echo "#define GPIO_RST_BUTTON_BIT                 16" >> include/config.h
	@echo "#define GPIO_RST_BUTTON_IS_ACTIVE_LOW        1" >> include/config.h
	@echo "#define DEFAULT_FLASH_SIZE_IN_MB             8" >> include/config.h
	@echo "#define BOARD_CUSTOM_STRING                  \"DB120 (AR9344) U-Boot for TL-WDR3500\"" >> include/config.h

	@./mkconfig -a db12x mips mips db12x ar7240 ar7240

mr3420_v2_config : unconfig wasp_common_config
	@/bin/echo -e '\e[32m> Configuring for TP-Link TL-MR3420 v2 at:' `date` '\e[0m'
	@echo "#define CONFIG_FOR_TPLINK_MR3420_V2          1" >> include/config.h
	@echo "#define CONFIG_AP123                         1" >> include/config.h
	@echo "#define DDR2_32BIT_SUPPORT                   1" >> include/config.h
	@echo "#define CFG_ATHRS27_PHY                      1" >> include/config.h
	@echo "#define CFG_AG7240_NMACS                     2" >> include/config.h
	@echo "#define GPIO_SYS_LED_BIT                    14" >> include/config.h
	@echo "#define GPIO_SYS_LED_ON                      0" >> include/config.h
	@echo "#define GPIO_WLAN_LED_BIT                   13" >> include/config.h
	@echo "#define GPIO_WLAN_LED_ON                     0" >> include/config.h
	@echo "#define GPIO_INTERNET_LED_BIT               18" >> include/config.h
	@echo "#define GPIO_INTERNET_LED_ON                 0" >> include/config.h
	@echo "#define GPIO_LAN1_LED_BIT                   19" >> include/config.h
	@echo "#define GPIO_LAN1_LED_ON                     0" >> include/config.h
	@echo "#define GPIO_LAN2_LED_BIT                   20" >> include/config.h
	@echo "#define GPIO_LAN2_LED_ON                     0" >> include/config.h
	@echo "#define GPIO_LAN3_LED_BIT                   21" >> include/config.h
	@echo "#define GPIO_LAN3_LED_ON                     0" >> include/config.h
	@echo "#define GPIO_LAN4_LED_BIT                   12" >> include/config.h
	@echo "#define GPIO_LAN4_LED_ON                     0" >> include/config.h
	@echo "#define GPIO_USB_LED_BIT                    11" >> include/config.h
	@echo "#define GPIO_USB_LED_ON                      0" >> include/config.h
	@echo "#define GPIO_QSS_LED_BIT                    15" >> include/config.h
	@echo "#define GPIO_QSS_LED_ON                      0" >> include/config.h
	@echo "#define GPIO_RST_BUTTON_BIT                 17" >> include/config.h
	@echo "#define GPIO_RST_BUTTON_IS_ACTIVE_LOW        1" >> include/config.h
	@echo "#define DEFAULT_FLASH_SIZE_IN_MB             4" >> include/config.h
	@echo "#define BOARD_CUSTOM_STRING                  \"AP123 (AR9341) U-Boot for TL-MR3420 v2\"" >> include/config.h

	@./mkconfig -a db12x mips mips db12x ar7240 ar7240

wr841n_v8_config : unconfig wasp_common_config
	@/bin/echo -e '\e[32m> Configuring for TP-Link TL-WR841N/D v8 at:' `date` '\e[0m'
	@echo "#define CONFIG_FOR_TPLINK_WR841N_V8          1" >> include/config.h
	@echo "#define CONFIG_AP123                         1" >> include/config.h
	@echo "#define DDR2_32BIT_SUPPORT                   1" >> include/config.h
	@echo "#define CFG_ATHRS27_PHY                      1" >> include/config.h
	@echo "#define CFG_AG7240_NMACS                     2" >> include/config.h
	@echo "#define GPIO_SYS_LED_BIT                    14" >> include/config.h
	@echo "#define GPIO_SYS_LED_ON                      0" >> include/config.h
	@echo "#define GPIO_WLAN_LED_BIT                   13" >> include/config.h
	@echo "#define GPIO_WLAN_LED_ON                     0" >> include/config.h
	@echo "#define GPIO_INTERNET_LED_BIT               18" >> include/config.h
	@echo "#define GPIO_INTERNET_LED_ON                 0" >> include/config.h
	@echo "#define GPIO_LAN1_LED_BIT                   19" >> include/config.h
	@echo "#define GPIO_LAN1_LED_ON                     0" >> include/config.h
	@echo "#define GPIO_LAN2_LED_BIT                   20" >> include/config.h
	@echo "#define GPIO_LAN2_LED_ON                     0" >> include/config.h
	@echo "#define GPIO_LAN3_LED_BIT                   21" >> include/config.h
	@echo "#define GPIO_LAN3_LED_ON                     0" >> include/config.h
	@echo "#define GPIO_LAN4_LED_BIT                   12" >> include/config.h
	@echo "#define GPIO_LAN4_LED_ON                     0" >> include/config.h
	@echo "#define GPIO_QSS_LED_BIT                    15" >> include/config.h
	@echo "#define GPIO_QSS_LED_ON                      0" >> include/config.h
	@echo "#define GPIO_RST_BUTTON_BIT                 17" >> include/config.h
	@echo "#define GPIO_RST_BUTTON_IS_ACTIVE_LOW        1" >> include/config.h
	@echo "#define DEFAULT_FLASH_SIZE_IN_MB             4" >> include/config.h
	@echo "#define BOARD_CUSTOM_STRING                  \"AP123 (AR9341) U-Boot for TL-WR841N/D v8\"" >> include/config.h

	@./mkconfig -a db12x mips mips db12x ar7240 ar7240

wa830re_v2_wa801nd_v2_config : unconfig wasp_common_config
	@/bin/echo -e '\e[32m> Configuring for TP-Link TL-WA830RE/TL-WA801ND v2 at:' `date` '\e[0m'
	@echo "#define CONFIG_FOR_TPLINK_WA830RE_V2_WA801ND_V2 1" >> include/config.h
	@echo "#define CONFIG_AP123                            1" >> include/config.h
	@echo "#define DDR2_32BIT_SUPPORT                      1" >> include/config.h
	@echo "#define CFG_ATHRS27_PHY                         1" >> include/config.h
	@echo "#define CFG_AG7240_NMACS                        2" >> include/config.h
	@echo "#define GPIO_SYS_LED_BIT                       14" >> include/config.h
	@echo "#define GPIO_SYS_LED_ON                         0" >> include/config.h
	@echo "#define GPIO_LAN_LED_BIT                       18" >> include/config.h
	@echo "#define GPIO_LAN_LED_ON                         0" >> include/config.h
	@echo "#define GPIO_WLAN_LED_BIT                      13" >> include/config.h
	@echo "#define GPIO_WLAN_LED_ON                        0" >> include/config.h
	@echo "#define GPIO_QSS_LED_BIT                       15" >> include/config.h
	@echo "#define GPIO_QSS_LED_ON                         0" >> include/config.h
	@echo "#define GPIO_RST_BUTTON_BIT                    17" >> include/config.h
	@echo "#define GPIO_RST_BUTTON_IS_ACTIVE_LOW           1" >> include/config.h
	@echo "#define DEFAULT_FLASH_SIZE_IN_MB                4" >> include/config.h
	@echo "#define BOARD_CUSTOM_STRING                     \"AP123 (AR9341) U-Boot for TL-WA830RE/TL-WA801ND v2\"" >> include/config.h

	@./mkconfig -a db12x mips mips db12x ar7240 ar7240

blink_sync_module_config : unconfig hornet_common_config
	@/bin/echo -e '\e[32m> Configuring for Immedia Semiconductor Blink Sync Module at:' `date` '\e[0m'
	@echo "#define CONFIG_FOR_BLINK_SYNC_MODULE         1" >> include/config.h
	@echo "#define GPIO_SYS_LED_BIT                    27" >> include/config.h
	@echo "#define GPIO_SYS_LED_ON                      0" >> include/config.h
	@echo "#define GPIO_WLAN_LED_BIT                    0" >> include/config.h
	@echo "#define GPIO_WLAN_LED_ON                     1" >> include/config.h
	@echo "#define GPIO_LAN1_LED_BIT                   14" >> include/config.h
	@echo "#define GPIO_LAN1_LED_ON                     1" >> include/config.h
	@echo "#define GPIO_LAN2_LED_BIT                   15" >> include/config.h
	@echo "#define GPIO_LAN2_LED_ON                     1" >> include/config.h
	@echo "#define GPIO_LAN3_LED_BIT                   16" >> include/config.h
	@echo "#define GPIO_LAN3_LED_ON                     1" >> include/config.h
	@echo "#define GPIO_LAN4_LED_BIT                   17" >> include/config.h
	@echo "#define GPIO_LAN4_LED_ON                     0" >> include/config.h
	@echo "#define GPIO_INTERNET_LED_BIT               13" >> include/config.h
	@echo "#define GPIO_INTERNET_LED_ON                 1" >> include/config.h
	@echo "#define GPIO_QSS_LED_BIT                     1" >> include/config.h
	@echo "#define GPIO_QSS_LED_ON                      1" >> include/config.h
	@echo "#define GPIO_RST_BUTTON_BIT                 11" >> include/config.h
	@echo "#define DEFAULT_FLASH_SIZE_IN_MB             4" >> include/config.h
	@echo "#define BOARD_CUSTOM_STRING                  \"U-Boot for Immedia Semi Blink Sync Module\"" >> include/config.h
	
	@./mkconfig -a ap121 mips mips ap121 ar7240 ar7240

#########################################################################
#########################################################################
#########################################################################

clean:
	@/bin/echo -e "\e[32m> Making $@...\e[0m"
	@find . -type f \
		\( -name 'core' -o -name '*.bak' -o -name '*~' \
		-o -name '*.o'  -o -name '*.a' -o -name .depend \) -print \
		| xargs rm -f
	@rm -f tools/mkimage tools/envcrc
	@rm -f lib_bootstrap/*.o
	@rm -f lib_bootstrap/*.a
	@rm -f bootstrap bootstrap.bin tuboot.bin u-boot.lzimg u-boot.bin.lzma bootstrap.map

clobber:	clean
	@/bin/echo -e "\e[32m> Making $@...\e[0m"
	@find . -type f \( -name .depend \
		-o -name '*.srec' -o -name '*.bin' -o -name u-boot.img \) \
		-print0 \
		| xargs -0 rm -f
	@rm -f $(OBJS) *.bak tags TAGS include/version_autogenerated.h
	@rm -fr *.*~
	@rm -f u-boot u-boot.map u-boot.hex $(ALL)
	@rm -f tools/crc32.c tools/environment.c
	@rm -f cpu/mpc824x/bedbug_603e.c
	@rm -f include/asm/proc include/asm/arch include/asm

mrproper \
distclean:	clobber unconfig

backup:
	F=`basename $(TOPDIR)` ; cd .. ; \
	gtar --force-local -zcvf `date "+$$F-%Y-%m-%d-%T.tar.gz"` $$F

#########################################################################
