1#!/bin/bash
2
3#
4# helper function to convert from DOS to Unix, if necessary, and handle
5# lines ending in '\'.
6#
7fix_newlines_in_macros() {
8	sed -n ':next;s/\r$//;/[^\\]\\$/ {N;s/\\\n//;b next};p' $1
9}
10
11#filter out only what we need from a10 hps.xml
12grep_a10_hps_config() {
13	grep -E "clk_hz|i_clk_mgr|i_io48_pin_mux|AXI_SLAVE|AXI_MASTER"
14}
15
16#
17# Process hps.xml
18# $1:	hps.xml
19# $2:	Output File
20#
21process_a10_hps_config() {
22	hps_xml="$1"
23	outfile="$2"
24
25	(cat << EOF
26// SPDX-License-Identifier: BSD-3-Clause
27/*
28 * Intel Arria 10 SoCFPGA configuration
29 */
30
31#ifndef __SOCFPGA_ARRIA10_CONFIG_H__
32#define __SOCFPGA_ARRIA10_CONFIG_H__
33
34EOF
35
36	echo "/* Clocks */"
37	fix_newlines_in_macros \
38		${hps_xml} | grep "clk_hz" |
39			awk -F"'" '{ gsub("\\.","_",$2) ; \
40				print "#define" " " toupper($2) " " $4}' |
41			sed 's/\.[0-9]//' |
42			sed 's/I_CLK_MGR_//' |
43			sort
44	fix_newlines_in_macros \
45		${hps_xml} | grep "i_clk_mgr_mainpll" |
46			awk -F"'" '{ gsub("\\.","_",$2) ; \
47				print "#define" " " toupper($2) " " $4}' |
48			sed 's/\.[0-9]//' |
49			sed 's/I_CLK_MGR_//' |
50			sort
51	fix_newlines_in_macros \
52		${hps_xml} | grep "i_clk_mgr_perpll" |
53			awk -F"'" '{ gsub("\\.","_",$2) ; \
54				print "#define" " " toupper($2) " " $4}' |
55			sed 's/\.[0-9]//' |
56			sed 's/I_CLK_MGR_//' |
57			sort
58	fix_newlines_in_macros \
59		${hps_xml} | grep "i_clk_mgr_clkmgr" |
60			awk -F"'" '{ gsub("\\.","_",$2) ; \
61				print "#define" " " toupper($2) " " $4}' |
62			sed 's/\.[0-9]//' |
63			sed 's/I_CLK_MGR_//' |
64			sort
65	fix_newlines_in_macros \
66		${hps_xml} | grep "i_clk_mgr_alteragrp" |
67			awk -F"'" '{ gsub("\\.","_",$2) ; \
68				print "#define" " " toupper($2) " " $4}' |
69			sed 's/\.[0-9]//' |
70			sed 's/I_CLK_MGR_//' |
71			sort
72	echo "#define ALTERAGRP_MPUCLK ((ALTERAGRP_MPUCLK_PERICNT << 16) | \\"
73	echo "	(ALTERAGRP_MPUCLK_MAINCNT))"
74	echo "#define ALTERAGRP_NOCCLK ((ALTERAGRP_NOCCLK_PERICNT << 16) | \\"
75	echo "	(ALTERAGRP_NOCCLK_MAINCNT))"
76
77	echo
78	echo "/* Pin Mux Configuration */"
79	fix_newlines_in_macros \
80		${hps_xml} | grep "i_io48_pin_mux" |
81			awk -F"'" '{ gsub("\\.","_",$2) ; \
82				print "#define" " " toupper($2) " " $4}' |
83			sed 's/I_IO48_PIN_MUX_//' |
84			sed 's/SHARED_3V_IO_GRP_//' |
85			sed 's/FPGA_INTERFACE_GRP_//' |
86			sed 's/DEDICATED_IO_GRP_//' |
87			sed 's/CONFIGURATION_DEDICATED/CONFIG/' |
88			sort
89
90	echo
91	echo "/* Bridge Configuration */"
92	fix_newlines_in_macros \
93		${hps_xml} | grep -E "AXI_SLAVE|AXI_MASTER" |
94			awk -F"'" '{ gsub("\\.","_",$2) ; \
95				print "#define" " " toupper($2) " " $4}' |
96			sed 's/true/1/' |
97			sed 's/false/0/' |
98			sort
99
100	echo
101	echo "/* Voltage Select for Config IO */"
102	echo "#define CONFIG_IO_BANK_VSEL \\"
103	echo "	(((CONFIG_IO_BANK_VOLTAGE_SEL_CLKRST_IO & 0x3) << 8) | \\"
104	echo "	(CONFIG_IO_BANK_VOLTAGE_SEL_PERI_IO & 0x3))"
105
106	echo
107	echo "/* Macro for Config IO bit mapping */"
108	echo -n "#define CONFIG_IO_MACRO(NAME) "
109	echo "(((NAME ## _RTRIM & 0xff) << 19) | \\"
110	echo "	((NAME ## _INPUT_BUF_EN & 0x3) << 17) | \\"
111	echo "	((NAME ## _WK_PU_EN & 0x1) << 16) | \\"
112	echo "	((NAME ## _PU_SLW_RT & 0x1) << 13) | \\"
113	echo "	((NAME ## _PU_DRV_STRG & 0xf) << 8) | \\"
114	echo "	((NAME ## _PD_SLW_RT & 0x1) << 5) | \\"
115	echo "	(NAME ## _PD_DRV_STRG & 0x1f))"
116
117	cat << EOF
118
119#endif /* __SOCFPGA_ARRIA10_CONFIG_H__ */
120EOF
121	) > "${outfile}"
122}
123
124usage() {
125	echo "$0 [hps_xml] [output_file]"
126	echo "Process QTS-generated hps.xml into devicetree header."
127	echo ""
128	echo "  hps_xml      - hps.xml file from hps_isw_handoff"
129	echo "  output_file  - Output header file for dtsi include"
130	echo ""
131}
132
133hps_xml="$1"
134outfile="$2"
135
136if [ "$#" -ne 2 ] ; then
137	usage
138	exit 1
139fi
140
141process_a10_hps_config "${hps_xml}" "${outfile}"
142