Merge tag 'u-boot-imx-20230923' of https://source.denx.de/u-boot/custodians/u-boot-imx
[platform/kernel/u-boot.git] / tools / zynqmp_psu_init_minimize.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0+
3 # Copyright (C) 2018 Michal Simek <michal.simek@amd.com>
4 # Copyright (C) 2019 Luca Ceresoli <luca@lucaceresoli.net>
5 # Copyright (C) 2022 Weidmüller Interface GmbH & Co. KG
6 # Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>
7
8 usage()
9 {
10     cat <<EOF
11
12 Transform a pair of psu_init_gpl.c and .h files produced by the Xilinx
13 Vivado tool for ZynqMP into a smaller psu_init_gpl.c file that is almost
14 checkpatch compliant. Minor coding style might still be needed. Must be
15 run from the top-level U-Boot source directory.
16
17 Usage:   zynqmp_psu_init_minimize.sh INPUT_DIR OUTPUT_DIR
18 Example: zynqmp_psu_init_minimize.sh \\
19                  /path/to/original/psu_init_gpl_c_and_h/ \\
20                  board/xilinx/zynqmp/<my_board>/
21
22 Notes:   INPUT_DIR must contain both .c and .h files.
23          If INPUT_DIR and OUTPUT_DIR are the same directory,
24          psu_init_gpl.c will be overwritten.
25
26 EOF
27 }
28
29 set -o errexit -o errtrace
30 set -o nounset
31
32 if [ $# -ne 2 ]
33 then
34     usage >&2
35     exit 1
36 fi
37
38 IN="${1}/psu_init_gpl.c"
39 OUT="${2}/psu_init_gpl.c"
40 TMP=$(mktemp /tmp/psu_init_gpl.XXXXXX)
41 trap "rm ${TMP}" ERR
42
43 # Step through a temp file to allow both $IN!=$OUT and $IN==$OUT
44 sed -e '/sleep.h/d' \
45     -e '/xil_io.h/d' \
46     ${IN} >${TMP}
47 cp ${TMP} ${OUT}
48
49 # preprocess to expand defines, then remove cpp lines starting with '#'
50 gcc -I${1} -E ${OUT} -o ${TMP}
51 sed '/^#/d' ${TMP} >${OUT}
52
53 # Remove trivial code before psu_pll_init_data()
54 sed -ni '/psu_pll_init_data/,$p' ${OUT}
55
56 # Functions are lowercase in U-Boot, rename them
57 sed -i 's/PSU_Mask_Write/psu_mask_write/g' ${OUT}
58 sed -i 's/mask_pollOnValue/mask_pollonvalue/g' ${OUT}
59 sed -i 's/RegValue/regvalue/g' ${OUT}
60 sed -i 's/MaskStatus/maskstatus/g' ${OUT}
61
62 sed -i '/&= psu_peripherals_powerdwn_data()/d' ${OUT}
63
64 FUNCS_TO_REMOVE="psu_protection
65 psu_..._protection
66 psu_init_xppu_aper_ram
67 mask_delay(u32
68 mask_read(u32
69 mask_poll(u32
70 mask_pollonvalue(u32
71 psu_ps_pl_reset_config_data
72 psu_ps_pl_isolation_removal_data
73 psu_apply_master_tz
74 psu_post_config_data
75 psu_post_config_data
76 psu_peripherals_powerdwn_data
77 psu_init_ddr_self_refresh
78 xmpu
79 xppu
80 "
81 for i in $FUNCS_TO_REMOVE; do
82 sed -i "/$i/,/^}$/d" ${OUT}
83 done
84
85 scripts/Lindent ${OUT}
86
87 # Prepend 'static' to internal functions
88 sed -i 's/^.*data(void)$/static &/g' ${OUT}
89 sed -i 's/^.*psu_afi_config(void)$/static &/g' ${OUT}
90 sed -i 's/^void init_peripheral/static &/g' ${OUT}
91 sed -i 's/^int serdes/static &/g' ${OUT}
92 sed -i 's/^int init_serdes/static &/g' ${OUT}
93 sed -i 's/^unsigned long /static &/g' ${OUT}
94
95 sed -i 's/()$/(void)/g' ${OUT}
96 sed -i 's/0X/0x/g' ${OUT}
97
98 # return (0) -> return 0
99 sed -ri 's/return \(([0-9]+)\)/return \1/g' ${OUT}
100
101 # Add header
102 cat << EOF >${TMP}
103 // SPDX-License-Identifier: GPL-2.0+
104 /*
105  * (c) Copyright 2015 Xilinx, Inc. All rights reserved.
106  */
107
108 #include <asm/arch/psu_init_gpl.h>
109 #include <xil_io.h>
110
111 EOF
112
113 cat ${OUT} >>${TMP}
114 cp ${TMP} ${OUT}
115
116 # Temporarily convert newlines to do some mangling across lines
117 tr "\n" "\r" <${OUT} >${TMP}
118
119 # Cleanup empty loops. E.g.:
120 # |while (e) {|
121 # |           | ==> |while (e)|
122 # |    }      |     |    ;    |
123 # |           |
124 sed -i -r 's| \{\r+(\t*)\}\r\r|\r\1\t;\r|g' ${TMP}
125
126 # Remove empty line between variable declaration
127 sed -i -r 's|\r(\r\t(unsigned )?int )|\1|g' ${TMP}
128
129 # Remove empty lines at function beginning/end
130 sed -i -e 's|\r{\r\r|\r{\r|g' ${TMP}
131 sed -i -e 's|\r\r}\r|\r}\r|g' ${TMP}
132
133 # Remove empty lines after '{' line
134 sed -i -e 's| {\r\r| {\r|g' ${TMP}
135
136 # Remove braces {} around single statement blocks. E.g.:
137 # | while (e) { |    | while (e) |
138 # |     stg();  | => |     stg();|
139 # | }           |
140 sed -i -r 's| \{(\r[^\r]*;)\r\t*\}|\1|g' ${TMP}
141
142 # Remove Unnecessary parentheses around 'n_code <= 0x3C' and similar. E.g.:
143 # if ((p_code >= 0x26) && ...) -> if (p_code >= 0x26 && ...)
144 sed -i -r 's|\((._code .= [x[:xdigit:]]+)\)|\1|g' ${TMP}
145
146 # Move helper functions below header includes
147 TARGET="#include <xil_io.h>"
148 START="static int serdes_rst_seq"
149 END="static int serdes_enb_coarse_saturation"
150
151 sed -i -e "s|\(${TARGET}\r\r\)\(.*\)\(${START}(.*\)\(${END}(\)|\1\3\2\4|g" \
152     ${TMP}
153
154 # Convert back newlines
155 tr "\r" "\n" <${TMP} >${OUT}
156
157 # Remove unnecessary settings
158 # - Low level UART
159 SETTINGS_TO_REMOVE="0xFF000000
160 0xFF000004
161 0xFF000018
162 0xFF000034
163 0xFF010000
164 0xFF010004
165 0xFF010018
166 0xFF010034
167 "
168 for i in $SETTINGS_TO_REMOVE; do
169 sed -i "/^\tpsu_mask_write($i,.*$/d" ${OUT}
170 done
171
172 rm ${TMP}