summaryrefslogtreecommitdiffstats
path: root/scripts/socfpga_import_preloader
blob: e917dcafefce60443c91e78a866293f9972523f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env bash

usage() {
	echo "USAGE: $0
	parameters:
	  -s|--spl-dir <SPL_GENERATED_DIR>
	  -i|--isw-handoff <ISW_HANDOFF>
	  -b|--board <BOARD_DIRECTORY>
	optional:
	  -e|--embedded-sdk <ALTERA_EMBEDDED_SDK>"
	echo "EXAMPLE: $0 -i ~/cv_soc_devkit_ghrd/hps_isw_handoff/soc_system_hps_0/ -b arch/arm/boards/altera-socdk -e ~/altera-embedded-sdk/"
	exit 1
}

die() {
	printf '%s\n' "$1" >&2
	exit 1
}

generate=
splroot=
embeddedsw=
handoff=
boardroot=

while :; do
	case $1 in
	-e|--embedded-sdk)
		if [ "$2" ]; then
			generate=1
			splroot="$(mktemp -d)"
			embeddedsw=${2}
			shift
		else
			die 'ERROR: "--embedded-sdk" requires a non-empty option argument.'
		fi
		;;
	-s|--spl-dir)
		if [ "$2" ]; then
			splroot="$2"
			shift
		else
			die 'ERROR: "--spl-dir" requires a non-empty option argument.'
		fi
		;;
	-i|--isw-handoff)
		if [ "$2" ]; then
			handoff="$2"
			shift
		else
			die 'ERROR: "--isw-handoff" requires a non-empty option argument.'
		fi
		;;
	-b|--board)
		if [ "$2" ]; then
			boardroot="$2"
			shift
		else
			die 'ERROR: "--board" requires a non-empty option argument.'
		fi
		;;
	*)
		break
	esac
	shift
done

bareboxsrc=.

cd ${bareboxsrc}

copy_source() {
	local src
	local tgt
	src=$1
	tgt=$2

	echo "Merging source code $src to $tgt"

	cp $src $tgt

	dos2unix $tgt

	echo "	Fixing conditional compilation..."
	unifdef -D HCX_COMPAT_MODE=1 -D ENABLE_INST_ROM_WRITE=1 $tgt -o $tgt

	echo "	Fixing extern/static keywords..."
	# Statify all global variables with missing static keyword
	sed -i 's/^const /static const /g' $tgt
	sed -i 's/^unsigned long sys_mgr_init_table/static unsigned long sys_mgr_init_table/g' $tgt

	echo "	Remove unused defines..."
	sed -i 's/\[CONFIG_HPS_PINMUX_NUM\]/\[\]/g' $tgt

	echo "	Translating altera int types..."
	# Replace altera types
	sed -i 's/alt_u32/uint32_t/g' $tgt
	sed -i 's/alt_u16/uint16_t/g' $tgt
	sed -i 's/alt_16/int16_t/g' $tgt
	sed -i 's/alt_32/int32_t/g' $tgt
	sed -i 's/alt_u8/uint8_t/g' $tgt
	sed -i 's/alt_8/int8_t/g' $tgt
	sed -i 's/#include "alt_types.h"//g' $tgt

	echo "	Fixing include paths..."
	# Fix include pathes
	sed -i 's/#include <iocsr_config_cyclone5.h>/#include <mach\/socfpga\/cyclone5-scan-manager.h>/g' $tgt
	sed -i 's/#include <pinmux_config.h>/#include <common.h>/g' $tgt
	sed -i 's/#include "sequencer_auto.h"//g' $tgt
	sed -i 's/#include "sequencer_defines.h"//g' $tgt

	echo "	Automated readability fixup..."
	indent -npro -kr -i8 -ts8 -sob -l100 -ss -ncs -cp1 -il0 $tgt
	sed -i 's/ $//g' $tgt
}

generate_spl() {
	USE_SOCEDS_PYTHON=1 SOCEDS_DESTROY_PATH=1 \
		${embeddedsw}/embedded/embedded_command_shell.sh python \
		${embeddedsw}/embedded/ip/altera/preloader/scripts/iswgen.py \
		-i ${handoff} -o ${splroot}/
}

if [ -z $splroot ] || [ -z $boardroot ] || [ -z $handoff ]; then
	usage
fi

if [ $generate ]; then
	generate_spl
fi

copy_source ${splroot}/iocsr_config_cyclone5.c ${boardroot}/iocsr_config_cyclone5.c
copy_source ${splroot}/pinmux_config_cyclone5.c ${boardroot}/pinmux_config.c
copy_source ${splroot}/pll_config.h ${boardroot}/pll_config.h
copy_source ${splroot}/sdram/sdram_config.h ${boardroot}/sdram_config.h

copy_source ${handoff}/sequencer_auto.h ${boardroot}/sequencer_auto.h
copy_source ${handoff}/sequencer_auto_ac_init.c ${boardroot}/sequencer_auto_ac_init.c
copy_source ${handoff}/sequencer_auto_inst_init.c ${boardroot}/sequencer_auto_inst_init.c
copy_source ${handoff}/sequencer_defines.h ${boardroot}/sequencer_defines.h

if [ $generate ]; then
	rm -r ${splroot}
fi

echo "DONE"