
#
# e2k/Makefile
#
# This file is included by the global makefile so that you can add your own
# architecture-specific flags and dependencies. Remember to do have actions
# for "archclean" and "archdep" for cleaning up and making dependencies for
# this architecture
#
# This file is subject to the terms and conditions of the GNU General Public
# License.  See the file "COPYING" in the main directory of this archive
# for more details.
#

# SHELL := $(shell if [ -x "$$BASH" ]; then echo $$BASH; \
#	  else if [ -x /bin/bash ]; then echo /bin/bash; \
#	  else if [ -x /usr/local/bin/bash ]; then echo /usr/local/bin/bash; \
#	  else echo sh; fi ; fi ; fi)
KBUILD_DEFCONFIG ?= defconfig

AS	= $(shell $(CC) -print-prog-name=as)
OBJDUMP	= $(shell $(CC) -print-prog-name=objdump)
LD	= $(shell $(CC) -print-prog-name=ld)
OBJCOPY	= $(shell $(CC) -print-prog-name=objcopy)

KBUILD_CFLAGS += -fkernel -gline -masm-inline $(call cc-option,-fforbid-fp)

ifeq ($(PROFILE_GENERATE), 1)
KBUILD_CFLAGS += -fprofile-generate-kernel
endif
ifeq ($(origin PROFILE_USE), undefined)
else
KBUILD_CFLAGS += -fprofile-use="$(PROFILE_USE)"
endif

KBUILD_CFLAGS += $(call cc-option,-finline-functions,) \
                 $(call cc-option,-finline-functions-called-once,)

# Some uninteresting or broken warnings can be disabled with #pragma's only
KBUILD_CFLAGS += -Wno-array-bounds -Wno-duplicate-type-qualifier \
		-Wno-builtin-functions-redefined -Wno-unused-value \
		-include $(srctree)/arch/e2k/include/asm/override-lcc-warnings.h

LDFLAGS_vmlinux :=
CHECKFLAGS	+= -D__e2k__

CFLAGS += -pipe -D__linux__

KBUILD_CFLAGS += $(CFLAGS)

ifeq  ($(call cc-lcc-yn),y)
  LCC_VERSION := $(shell $(CC) -v 2>&1 | grep lcc | cut -d. -f 2)
  LCC_120 := $(shell if test "$(LCC_VERSION)" = "A"; then echo "y"; \
		else if test $(LCC_VERSION) -gt 19; then echo "y"; \
		else echo "n"; fi; fi; )
  LCC_122 := $(shell if test "$(LCC_VERSION)" = "A"; then echo "y"; \
		else if test $(LCC_VERSION) -gt 21; then echo "y"; \
		else echo "n"; fi; fi; )
  LCC_123 := $(shell if test "$(LCC_VERSION)" = "A"; then echo "y"; \
		else if test $(LCC_VERSION) -gt 22; then echo "y"; \
		else echo "n"; fi; fi; )
  export LCC_122
  export LCC_123
  ifeq ($(LCC_120),n)
    ifdef CONFIG_E2S_CPU_RF_BUG
     KBUILD_CFLAGS += -ffix-hw-bug67911
    endif
  endif
endif
ifdef CONFIG_SMP_DAM_BUG
 KBUILD_CFLAGS += -fno-dam-call
endif

ifeq ($(LCC_123),y)
CFLAGS_GENERIC	:= -march=elbrus-v2
CFLAGS_ES2	:= -mtune=elbrus-2c+
CFLAGS_E2S	:= -mtune=elbrus-4c
CFLAGS_E8C	:= -mtune=elbrus-8c
CFLAGS_E1CP	:= -mtune=elbrus-1c+
CFLAGS_E8C2	:= -mtune=elbrus-8c2
CFLAGS_E12C	:= -mtune=elbrus-12c
CFLAGS_E16C	:= -mtune=elbrus-16c
CFLAGS_E2C3	:= -mtune=elbrus-2c3

CFLAGS_ALL_CPUS	:= $(CFLAGS_ES2) $(CFLAGS_E2S) $(CFLAGS_E8C) $(CFLAGS_E1CP) \
		   $(CFLAGS_E8C2) $(CFLAGS_E12C) $(CFLAGS_E16C) $(CFLAGS_E2C3)
export CFLAGS_ALL_CPUS
else
# Avoid false warning about missing return after __builtin_unreachable()
KBUILD_CFLAGS += -Wno-return-type

CFLAGS_GENERIC	:= -mcpu=elbrus-v2
CFLAGS_ES2	:= -mcpu=elbrus-2c+
CFLAGS_E2S	:= -mcpu=elbrus-4c
CFLAGS_E8C	:= -mcpu=elbrus-8c
CFLAGS_E1CP	:= -mcpu=elbrus-1c+
CFLAGS_E8C2	:= -mcpu=elbrus-8c2
CFLAGS_E12C	:= $(CFLAGS_E8C2)
CFLAGS_E16C	:= $(CFLAGS_E8C2)
CFLAGS_E2C3	:= $(CFLAGS_E8C2)
endif

CFLAGS_E2K_SIC	:= $(CFLAGS_ES2)

export	CFLAGS_ES2 CFLAGS_E2S CFLAGS_E8C CFLAGS_E1CP CFLAGS_E8C2 CFLAGS_E2C3 \
	CFLAGS_E12C CFLAGS_E16C CFLAGS_E2K_SIC

ifeq ($(CONFIG_E2K_MACHINE),y)
 ifeq ($(CONFIG_E2K_ES2_DSP),y)
 KBUILD_CFLAGS += $(CFLAGS_ES2)
 KBUILD_AFLAGS += $(CFLAGS_ES2)
 TARGET_MDL    := 04
 else
  ifeq ($(CONFIG_E2K_ES2_RU),y)
  KBUILD_CFLAGS	+= $(CFLAGS_ES2)
  KBUILD_AFLAGS	+= $(CFLAGS_ES2)
  TARGET_MDL    := 06
  else
   ifeq ($(CONFIG_E2K_E2S),y)
   KBUILD_CFLAGS += $(CFLAGS_E2S)
   KBUILD_AFLAGS += $(CFLAGS_E2S)
   TARGET_MDL    := 03
   else
    ifeq ($(CONFIG_E2K_E8C),y)
    KBUILD_CFLAGS += $(CFLAGS_E8C)
    KBUILD_AFLAGS += $(CFLAGS_E8C)
    TARGET_MDL    := 07
    else
     ifeq ($(CONFIG_E2K_E1CP),y)
     KBUILD_CFLAGS += $(CFLAGS_E1CP)
     KBUILD_AFLAGS += $(CFLAGS_E1CP)
     TARGET_MDL    := 08
     else
      ifeq ($(CONFIG_E2K_E8C2),y)
      KBUILD_CFLAGS += $(CFLAGS_E8C2)
      KBUILD_AFLAGS += $(CFLAGS_E8C2)
      TARGET_MDL    := 09
      else
       ifeq ($(CONFIG_E2K_E12C),y)
       KBUILD_CFLAGS += $(CFLAGS_E12C)
       KBUILD_AFLAGS += $(CFLAGS_E12C)
       TARGET_MDL    := 0a
       else
        ifeq ($(CONFIG_E2K_E16C),y)
        KBUILD_CFLAGS += $(CFLAGS_E16C)
        KBUILD_AFLAGS += $(CFLAGS_E16C)
        TARGET_MDL    := 0b
        else
         ifeq ($(CONFIG_E2K_E2C3),y)
         KBUILD_CFLAGS += $(CFLAGS_E2C3)
         KBUILD_AFLAGS += $(CFLAGS_E2C3)
         TARGET_MDL    := 0c
         else
          error "Invalid e2k machine type"
         endif	# ifeq ($(CONFIG_E2K_E2C3),y)
        endif	# ifeq ($(CONFIG_E2K_E16C),y)
       endif	# ifeq ($(CONFIG_E2K_E12C),y)
      endif	# ifeq ($(CONFIG_E2K_E8C2),y)
     endif	# ifeq ($(CONFIG_E2K_E1CP),y)
    endif	# ifeq ($(CONFIG_E2K_E8C),y)
   endif	# ifeq ($(CONFIG_E2K_E2S),y)
  endif		# ifeq ($(CONFIG_E2K_ES2_RU),y)
 endif		# ifeq ($(CONFIG_E2K_ES2_DSP),y)
else		# ! ifeq ($(CONFIG_E2K_MACHINE),y)
KBUILD_CFLAGS	+= $(CFLAGS_GENERIC)
KBUILD_AFLAGS	+= $(CFLAGS_GENERIC)
TARGET_MDL      := 00
endif

LDFLAGS		+= --relaxed-e2k-machine-check

obj-$(CONFIG_KPROBES)	+= kprobes.o

KBUILD_CFLAGS += $(cflags-y)

libs-y 					+= arch/e2k/lib/
core-y					+= arch/e2k/kernel/ \
					   arch/e2k/mm/ \
					   arch/e2k/p2v/ \
					   arch/e2k/fast_syscalls/

core-$(CONFIG_PROTECTED_MODE)		+= arch/e2k/3p/

drivers-$(CONFIG_PCI)			+= arch/e2k/pci/ 

# suspend and hibernation support
drivers-$(CONFIG_PM)			+= arch/e2k/power/

#KVM hypervisor and guest support
core-$(CONFIG_KVM)			+= arch/e2k/kvm/
core-$(CONFIG_KVM)			+= arch/e2k/kvm/guest/

# Elbrus common modules
core-y					+= arch/l/
drivers-$(CONFIG_PCI)			+= arch/l/pci/

boot :=   arch/e2k/boot
ES2ROM=es2rom
E2SROM=e2srom
E8CROM=e8crom
E1CPROM=e1cprom
E8C2ROM=e8c2rom
E12CROM=e12crom
E16CROM=e16crom
E2C3ROM=e2c3rom

all: es2rom

MAKEBOOT = $(MAKE) -C arch/$(ARCH)/boot

.PHONY:	clean archclean archmrproper archdep bootimage image zImage

es2rom: vmlinux  
	$(Q)$(MAKE) $(build)=$(boot) CONFIG_ES2=y $(ES2ROM)

e2srom: vmlinux
	$(Q)$(MAKE) $(build)=$(boot) CONFIG_E2S=y $(E2SROM)

e8crom: vmlinux
	$(Q)$(MAKE) $(build)=$(boot) CONFIG_E8C=y $(E8CROM)

e1cprom: vmlinux
	$(Q)$(MAKE) $(build)=$(boot) CONFIG_E1CP=y $(E1CPROM)

e8c2rom: vmlinux
	$(Q)$(MAKE) $(build)=$(boot) CONFIG_E8C2=y CONFIG_E8C=y $(E8C2ROM)

e12crom: vmlinux
	$(Q)$(MAKE) $(build)=$(boot) CONFIG_E12C=y $(E12CROM)

e16crom: vmlinux
	$(Q)$(MAKE) $(build)=$(boot) CONFIG_E16C=y $(E16CROM)

e2c3rom: vmlinux
	$(Q)$(MAKE) $(build)=$(boot) CONFIG_E2C3=y $(E2C3ROM)

image: vmlinux
	$(Q)$(MAKE) $(build)=$(boot) CONFIG_BOOT=y $(objtree)/image.boot
	$(Q)echo "Target mdl: $(TARGET_MDL)"; \
	echo $(TARGET_MDL) | \
		xxd -r -p | \
		dd of=$(objtree)/image.boot bs=1 seek=258 count=1 conv=notrunc 2>/dev/null; \
	e2k_kernel_csum=`cksum $(objtree)/image.boot | awk '{ printf "%08x\n", $$1 }'`; \
	echo "Kernel image check sum: $$e2k_kernel_csum"; \
	echo $$e2k_kernel_csum | \
		sed 's/\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\7\8\5\6\3\4\1\2/' | \
		xxd -r -p | \
		dd of=$(objtree)/image.boot bs=1 seek=1588 count=4 conv=notrunc 2>/dev/null; \
	echo 'Kernel: image.boot is ready' ' (#'`cat .version`')'
	@unset e2k_kernel_csum;

zImage: vmlinux
	$(Q)$(MAKE) $(build)=$(boot) CONFIG_BOOT=y $(objtree)/zImage
	$(Q)echo "Target mdl: $(TARGET_MDL)"; \
	echo $(TARGET_MDL) | \
		xxd -r -p | \
		dd of=$(objtree)/zImage bs=1 seek=258 count=1 conv=notrunc 2>/dev/null; \
	e2k_kernel_csum=`cksum $(objtree)/zImage | awk '{ printf "%08x\n", $$1 }'`; \
	echo "Kernel image check sum: $$e2k_kernel_csum"; \
	echo $$e2k_kernel_csum | \
		sed 's/\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\7\8\5\6\3\4\1\2/' | \
		xxd -r -p | \
		dd of=$(objtree)/zImage bs=1 seek=1588 count=4 conv=notrunc 2>/dev/null; \
	echo 'Kernel: zImage is ready' ' (#'`cat .version`')'
	@unset e2k_kernel_csum;

image.boot: bootimage
bootimage: image

archclean:
	$(Q)$(MAKE) $(clean)=arch/e2k/boot

archmrproper:

archdep:
	@$(MAKEBOOT) dep

install-headers: 
	@$(MAKEBOOT) install-headers

install-includes: include/linux/version.h arch/e2k/include FORCE
	$(CONFIG_SHELL) scripts/gen-osl-include -l $(srctree) -r $(ROOT_WA)

build-install: FORCE
	$(CONFIG_SHELL) scripts/gen-osl-build -l $(srctree) -m $(MODLIB)

define archhelp
  echo  '* image/bootimage	- Kernel boot image (image.boot)'
  echo  '  zImage		- Compressed kernel boot image (image.boot)'
  echo  '  install-headers	- Install kernel headers in '
  echo  '                          <basedir>/usr/include'
  echo  '  es2rom		- Build kernel ROM image with existing boot for es2 (cubic)'
  echo  '  e2srom		- Build kernel ROM image with existing boot for e2s'
  echo  '  e8crom		- Build kernel ROM image with existing boot for e8c'
  echo  '  e1cprom		- Build kernel ROM image with existing boot for e1c+'
  echo  '  e8c2rom		- Build kernel ROM image with existing boot for e8c2'
  echo  '  e12crom		- Build kernel ROM image with existing boot for e12c'
  echo  '  e16crom		- Build kernel ROM image with existing boot for e16c'
  echo  '  e2c3rom		- Build kernel ROM image with existing boot for e2c3'
endef
