###############################################################################
#
#  BRIEF MODULE DESCRIPTION
#     Makefile create a compressed zImage or Rommable rImage
#
#  Copyright 2004 IDT Inc. (rischelp@idt.com)
#
#  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  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
#  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
#   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
#   NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
#   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
#   NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
#   USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
#   ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
#   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
#   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#   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.,
#   675 Mass Ave, Cambridge, MA 02139, USA.
# 
# 
###############################################################################
# Oct 29 2002
# P. Sadik   (Pallathu.Sadik@idt.com)
###############################################################################
# P. Sadik  Oct 07 2003
#
# Modified to make  zImage bootable
###############################################################################
# P. Sadik  Oct 08 2003
#
# Removed zImage.o rule.
###############################################################################

##################################################################
# The following is taken from IDT/Sim Makefile
##################################################################
TARGET=438
TARGETDIR=S438
MHZ=100000000

#
# following refers to memory type in use in eval board and if more than one
# then the order is implied.  These are values for the switch MEMCFG.
#
SRAM_ONLY=1
SDRAM_ONLY=2
SRAM_N_SDRAM=3
SDRAM_N_SRAM=4

# following refers to size of the DRAM space.
# These are values for the switch DRAMSZ.

MB32=1
MB64=2
MB128=3
MB32SO=4

MACH= -DEB438 -DS438 -DCPU_R32438 -DMIPSEL -DINET -DMEMCFG=$(SDRAM_ONLY) -DDRAMSZ=$(MB64) -DIDTSIM -DMHZ=$(MHZ) -DNVRAM_RTC -DUSE_SPI
COMMSWITCHES = $(MACH)
#***************** END IDT/Sim Makefile ##################################### 
ZDEBUG=0
export ZDEBUG

include $(TOPDIR)/.config

SIZE = $(CROSS_COMPILE)size

O_FORMAT = $(shell $(OBJDUMP) -i | head -2 | grep elf32)
OBJDUMP_VERSION := $(shell if $(OBJDUMP) -i | head -2 2>&1 | \
			grep 'elf32-trad' >/dev/null; then echo y;else echo n;fi)
export OBJDUMP_VERSION

SYSTEM	      := $(TOPDIR)/vmlinux
ZBSS          := -Tbss 0x800A0000

ifdef CONFIG_IDT_FLASH_ADDR
ZIMSTART      := $(CONFIG_IDT_FLASH_ADDR)
else
ZIMSTART      := 0x91000000
endif
BFCSTART      := 0x9FC00000
# working space for gunzip:
FREE_RAM      := 0x80C00000
END_RAM       := 0x80E00000

COMMFLAGS  = -static $(ZBSS) -T $(TOPDIR)/arch/mips/ld.script
RLINKFLAGS = -Ttext $(BFCSTART) -e start $(COMMFLAGS)
ZLINKFLAGS = -Ttext $(ZIMSTART) -e zstartup  $(COMMFLAGS)

# Some versions of mipsel-linux-objdump displays addresses starting with 0xffffffff.
# Hence strip that portion of the address.
ifneq ($(OBJDUMP_VERSION),y)
LOADADDR      := 0x$(shell $(NM) $(SYSTEM) | \
			grep _ftext |cut -f1 -d' ' |sed -n -e 's/ffffffff//p')
KERNEL_ENTRY  := $(shell $(OBJDUMP) -f $(SYSTEM) | \
			sed -n -e 's/^start address //p' |sed -n -e 's/ffffffff//p')
else
LOADADDR      := 0x$(shell $(NM) $(SYSTEM) | grep _ftext |cut -f1 -d' ')
KERNEL_ENTRY  := $(shell $(OBJDUMP) -f $(SYSTEM) | sed -n -e 's/^start address //p')
endif

####################################################################################
OBJECTS := piggy.o head.o misc.o
ifneq ($(ZDEBUG),0)
OBJECTS += uart16550.o
endif

.PHONY: zImage rImage
zImage: $(TOPDIR)/zImage
rImage: $(TOPDIR)/rImage

head.o: head.S Makefile $(SYSTEM)
	$(CC) $(CFLAGS) -DKERNEL_ENTRY=$(KERNEL_ENTRY) -c head.S

misc.o: misc.c Makefile
	$(CC) $(CFLAGS) -DLOADADDR=$(LOADADDR) -DFREE_RAM=$(FREE_RAM) -DEND_RAM=$(END_RAM) -c misc.c -DZDEBUG=$(ZDEBUG)

piggy.o: $(SYSTEM)
	$(OBJCOPY) -S -O binary -R .note -R .comment $(SYSTEM) $(SYSTEM).bin
	gzip -f -9 < $(SYSTEM).bin > $(SYSTEM).gz
	echo "OUTPUT_ARCH(mips) SECTIONS { .initrd : { input_len = .; LONG(input_data_end - input_data) \
		input_data = .; *(.data) input_data_end = .; }}" > ld.script
	echo "O_FORMAT:  " $(O_FORMAT); 
	$(LD) -T ld.script -b binary --oformat $(O_FORMAT) -o piggy.o $(SYSTEM).gz
	rm -f $(SYSTEM).bin $(SYSTEM).gz ld.script

$(TOPDIR)/zImage: $(OBJECTS)
	$(LD) $(ZLINKFLAGS) -o $(TOPDIR)/zImage $(OBJECTS)
	$(OBJCOPY) -S -O binary -R .note -R .comment $(TOPDIR)/zImage $(TOPDIR)/zImage.bin
	$(OBJCOPY) -I binary -S -O srec --srec-forceS3 --srec-len=32 --change-start=0x00000000 \
		 $(TOPDIR)/zImage.bin $(TOPDIR)/zImage.prm
	$(SIZE) $(TOPDIR)/zImage |awk -F" " '{ print $$4 "\t" $$5 }' > $(TOPDIR)/zImage.size
	rm -f *.o

csu_idt.o: csu_idt.S Makefile $(SYSTEM)
	$(CC) $(AFLAGS) $(UFLAGS) $(COMMSWITCHES) -c csu_idt.S


ifdef  CONFIG_IDT_BOOT_FROM_PROM
$(TOPDIR)/rImage: $(OBJECTS) csu_idt.o
	@rm -f $(TOPDIR)/*.prm
	$(LD) $(RLINKFLAGS) -o $(TOPDIR)/rImage csu_idt.o $(OBJECTS) 
	$(OBJCOPY) -S -O binary -R .note -R .comment $(TOPDIR)/rImage $(TOPDIR)/rImage.bin
	$(OBJCOPY) -I binary -S -O srec --srec-forceS3 --srec-len=32 --change-start=0x00000000 \
		 $(TOPDIR)/rImage.bin $(TOPDIR)/linux.prm
	$(SIZE) $(TOPDIR)/rImage |awk -F" " '{ print $$4 "\t" $$5 }' > $(TOPDIR)/linux.size
	rm -f *.o
else
$(TOPDIR)/rImage: $(SYSTEM)
	@echo
	@echo "Error! Re-configure kernel with 'Create ROMMABLE Image' option enabled."
	@echo
endif

clean:
	rm -f *.o $(TOPDIR)/zImage $(TOPDIR)/rImage $(TOPDIR)/rImage.bin $(TOPDIR)/*.prm $(TOPDIR)/linux.size
