1###############################################################################
2#
3# Altera SoC EDS Preloader/UBoot Build System
4#
5# Copyright (c) 2013,2014 Altera Corporation
6# All Rights Reserved
7#
8###############################################################################
9
10
11###############################################################################
12#
13# Generated Variables
14#
15
16PRELOADER_SETTINGS_DIR := ../../hps_isw_handoff/soc_system_hps_0
17
18TGZ := $(SOCEDS_DEST_ROOT)/host_tools/altera/preloader/uboot-socfpga.tar.gz
19
20CROSS_COMPILE := arm-altera-eabi-
21
22DEVICE_FAMILY := cyclone5
23
24###############################################################################
25
26
27###############################################################################
28#
29# Derived Variables
30#
31
32MAKE_ARGS += CROSS_COMPILE=$(CROSS_COMPILE)
33
34PRELOADER_SRC_DIR := $(patsubst %.tar.gz,%,$(shell basename $(TGZ)))
35
36PRELOADER_UPDATE_DIR := $(PRELOADER_SRC_DIR)/board/altera/socfpga
37
38SOCFPGA_BOARD_CONFIG := socfpga_$(DEVICE_FAMILY)_config
39
40###############################################################################
41
42
43###############################################################################
44#
45# Tools
46#
47
48CAT := cat
49CHMOD := chmod
50CP := cp -rf
51DIFF := diff
52ECHO := echo
53PATCH := patch
54MKDIR := mkdir -p
55RM := rm -rf
56TOUCH := touch
57UNTAR := tar zxf
58
59###############################################################################
60
61
62###############################################################################
63#
64# Helper Variables and Functions
65#
66
67STAMP_DIR ?= $(PRELOADER_SRC_DIR)
68
69define stamp
70@$(MKDIR) $(@D)
71@$(TOUCH) $@
72endef
73
74define untar_recipe
75$(UNTAR) $(if $1,$1,$(if $<,$<,$(error ERROR: no input provided to gnu make function untar_recipe)))
76endef
77
78# Stamps
79UNTAR_SRC := $(STAMP_DIR)/.untar
80CONFIG := $(STAMP_DIR)/.config
81PATCH_APPLY := $(STAMP_DIR)/.patch
82
83###############################################################################
84
85
86###############################################################################
87#
88# Windows Support
89#
90#
91HOSTOS := $(shell uname -o 2>/dev/null | tr [:upper:] [:lower:])
92
93ifeq ($(HOSTOS),cygwin)
94
95# When using UBoot build system on Windows it's good idea to use cygwin's GNU make
96MAKE := $(shell cygpath -m "/bin/make")
97MAKE_ARGS += MAKE=/bin/make
98
99CYGPATH := $(shell cygpath -m "$(shell which cygpath)")
100MAKE_ARGS += CYGPATH=$(CYGPATH)
101
102UNAME_M := $(shell uname -m)
103ifeq ($(UNAME_M),x86_64)
104HOST_CROSS_COMPILE := x86_64-w64-mingw32-
105else
106HOST_CROSS_COMPILE := i686-pc-mingw32-
107endif
108
109MAKE_ARGS += HOSTCC=$(HOST_CROSS_COMPILE)gcc HOSTSTRIP=$(HOST_CROSS_COMPILE)strip
110
111# Under cygwin, overload the untar_recipe function to use unix stype paths. This is required for cygwin tar
112define untar_recipe
113$(UNTAR) $(shell cygpath --unix "$(if $1,$1,$(if $<,$<,$(error ERROR: no input provided to gnu make function untar_recipe)))")
114endef
115
116else # if HOSTOS != cygwin
117
118ifdef WINDIR
119$(error ERROR: Windows build of preloader requires cygwin build environment. Ensure this makefile is executed from the SoC EDS Command Shell)
120endif
121ifdef windir
122$(error ERROR: Windows build of preloader requires cygwin build environment. Ensure this makefile is executed from the SoC EDS Command Shell)
123endif
124
125endif # HOSTOS == cygwin
126###############################################################################
127
128
129###############################################################################
130#
131# Source Files
132#
133
134SDRAM_SOURCE_FILES := \
135	alt_types.h \
136	sdram_io.h \
137	sequencer_auto_ac_init.c \
138	sequencer_auto.h \
139	sequencer_auto_inst_init.c \
140	sequencer.c \
141	sequencer_defines.h \
142	sequencer.h \
143	system.h \
144	tclrpt.c \
145	tclrpt.h
146
147GENERATED_SOURCE_FILES := \
148	build.h \
149	iocsr_config_$(DEVICE_FAMILY).c \
150	iocsr_config_$(DEVICE_FAMILY).h \
151	reset_config.h \
152	pll_config.h \
153	pinmux_config_$(DEVICE_FAMILY).c \
154	pinmux_config.h \
155	sdram/sdram_config.h
156
157
158UPDATE_SDRAM_SOURCE_FILES := $(patsubst %,$(PRELOADER_UPDATE_DIR)/sdram/%,$(SDRAM_SOURCE_FILES))
159
160UPDATE_GENERATED_SOURCE_FILES := $(patsubst %,$(PRELOADER_UPDATE_DIR)/%,$(GENERATED_SOURCE_FILES))
161
162###############################################################################
163
164
165###############################################################################
166#
167# Main build targets
168#
169
170PRELOADER.BINARY := $(PRELOADER_SRC_DIR)/spl/u-boot-spl.bin
171PRELOADER.MKPIMAGE_BINARY := preloader-mkpimage.bin
172UBOOT.BINARY := $(PRELOADER_SRC_DIR)/u-boot.bin
173
174.PHONY: all
175all: spl mkpimage-spl
176
177.PHONY: uboot
178uboot: $(UBOOT.BINARY)
179
180$(UBOOT.BINARY): $(CONFIG)
181	$(MAKE) $(MAKE_ARGS) -C $(PRELOADER_SRC_DIR)
182
183.PHONY: spl
184spl: $(PRELOADER.BINARY)
185
186$(PRELOADER.BINARY): $(CONFIG)
187	$(MAKE) $(MAKE_ARGS) -C $(PRELOADER_SRC_DIR) spl/u-boot-spl.bin
188
189.PHONY: mkpimage-spl
190mkpimage-spl: $(PRELOADER.MKPIMAGE_BINARY)
191
192$(PRELOADER.MKPIMAGE_BINARY): $(PRELOADER.BINARY)
193	mkpimage --header-version 0 -o $@ $< $< $< $<
194
195.PHONY: tools
196tools: $(CONFIG)
197	$(MAKE) $(MAKE_ARGS) -C $(PRELOADER_SRC_DIR) tools
198
199
200###############################################################################
201
202
203###############################################################################
204#
205# Untar, Update, & Configure Preloader Source
206#
207
208
209#####
210# Untar
211.PHONY: src
212src: $(UNTAR_SRC)
213
214$(UNTAR_SRC): $(TGZ)
215	@$(RM) $(PRELOADER_SRC_DIR)
216	$(untar_recipe)
217	@$(CHMOD) -R 755 $(PRELOADER_SRC_DIR)
218	$(stamp)
219
220#####
221# Update
222.PHONY: update-src
223update-src: $(UPDATE_SRC)
224
225UPDATE_SRC += $(UPDATE_GENERATED_SOURCE_FILES) $(UPDATE_SDRAM_SOURCE_FILES)
226
227$(UPDATE_SDRAM_SOURCE_FILES): $(PRELOADER_UPDATE_DIR)/sdram/%: $(PRELOADER_SETTINGS_DIR)/% $(UNTAR_SRC) $(PATCH.APPLY_TARGETS)
228	@$(MKDIR) $(@D)
229	@$(CP) -v $< $@
230
231$(UPDATE_GENERATED_SOURCE_FILES): $(PRELOADER_UPDATE_DIR)/%: generated/% $(UNTAR_SRC) $(PATCH.APPLY_TARGETS)
232	@$(MKDIR) $(@D)
233	@$(CP) -v $< $@
234
235#####
236# Configure
237.PHONY: config
238config: $(CONFIG)
239
240$(CONFIG): $(UPDATE_SRC) $(UNTAR_SRC) $(PATCH_APPLY)
241	$(MAKE) $(MAKE_ARGS) -C $(PRELOADER_SRC_DIR) $(SOCFPGA_BOARD_CONFIG)
242	$(stamp)
243
244###############################################################################
245
246
247###############################################################################
248#
249# Cleaning up
250#
251
252.PHONY: clean
253clean:
254ifneq ($(wildcard $(PRELOADER_SRC_DIR)),)
255	$(MAKE) $(MAKE_ARGS) -C $(PRELOADER_SRC_DIR) mrproper
256endif
257	$(RM) $(PRELOADER.MKPIMAGE_BINARY) $(CONFIG)
258
259.PHONY: clean-all
260clean-all:
261	$(RM) $(PRELOADER_SRC_DIR) $(PRELOADER.MKPIMAGE_BINARY) $(CONFIG) $(PATCH_APPLY) $(UNTAR_SRC)
262
263###############################################################################
264
265
266###############################################################################
267#
268# Applying patch files
269#
270
271# GNU MAKE >= 3.81 is required to apply patch files correctly
272.SECONDEXPANSION:
273
274# Patch files are discovered in current directory and in the directory adjacent
275# to the tarball (TGZ) directory
276
277PATCH.FILES := $(strip \
278	$(sort $(wildcard $(patsubst %.tar.gz,%.patch,$(TGZ))/*.patch)) \
279	$(sort $(wildcard $(patsubst %.tar.gz,%.patch,$(TGZ))/$(HOSTOS)/*.patch)) \
280	$(sort $(wildcard $(abspath .)/*.patch)) \
281	$(EXTRA_PATCH_FILES))
282
283PATCH.APPLY_TARGETS := $(strip $(foreach patchfile,$(PATCH.FILES), \
284 $(eval patchfile_target := $(notdir $(basename $(patchfile)))) \
285 $(eval $(patchfile_target).PATCH_FILE := $(patchfile)) \
286 $(PRELOADER_SRC_DIR)/.applypatch.$(patchfile_target) \
287))
288
289.PHONY: patch-apply
290patch-apply: $(PATCH_APPLY)
291
292$(PATCH_APPLY): $(PATCH.APPLY_TARGETS)
293	$(stamp)
294
295$(PATCH.APPLY_TARGETS): $(PRELOADER_SRC_DIR)/.applypatch.%: $$(%.PATCH_FILE) $(UNTAR_SRC)
296	@$(ECHO) Applying Patch: $<
297	$(PATCH) -p1 --directory=$(PRELOADER_SRC_DIR) --input=$<
298	$(stamp)
299
300###############################################################################
301
302
303###############################################################################
304#
305# Creating a patch file
306#
307
308PATCH.USER_FILE := user.patch
309
310.PHONY: patch-create
311patch-create: $(if $(PATCH.SKIP_CLEAN),,clean)
312ifeq ($(wildcard $(PRELOADER_SRC_DIR).orig),)
313	$(error ERROR: $(PRELOADER_SRC_DIR).orig does not exist)
314endif
315	$(DIFF) -rupN $(PRELOADER_SRC_DIR).orig/ $(PRELOADER_SRC_DIR)/ > $(PATCH.USER_FILE) || true
316ifeq ($(HOSTOS),cygwin)
317	dos2unix $(PATCH.USER_FILE)
318endif
319	$(CAT) $(PATCH.USER_FILE)
320
321###############################################################################
322
323
324