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
8usage()
9{
10    cat <<EOF
11
12Transform a pair of psu_init_gpl.c and .h files produced by the Xilinx
13Vivado tool for ZynqMP into a smaller psu_init_gpl.c file that is almost
14checkpatch compliant. Minor coding style might still be needed. Must be
15run from the top-level U-Boot source directory.
16
17Usage:   zynqmp_psu_init_minimize.sh INPUT_DIR OUTPUT_DIR
18Example: zynqmp_psu_init_minimize.sh \\
19                 /path/to/original/psu_init_gpl_c_and_h/ \\
20                 board/xilinx/zynqmp/<my_board>/
21
22Notes:   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
26EOF
27}
28
29set -o errexit -o errtrace
30set -o nounset
31
32if [ $# -ne 2 ]
33then
34    usage >&2
35    exit 1
36fi
37
38IN="${1}/psu_init_gpl.c"
39OUT="${2}/psu_init_gpl.c"
40TMP=$(mktemp /tmp/psu_init_gpl.XXXXXX)
41trap "rm ${TMP}" ERR
42
43# Step through a temp file to allow both $IN!=$OUT and $IN==$OUT
44sed -e '/sleep.h/d' \
45    -e '/xil_io.h/d' \
46    ${IN} >${TMP}
47cp ${TMP} ${OUT}
48
49# preprocess to expand defines, then remove cpp lines starting with '#'
50gcc -I${1} -E ${OUT} -o ${TMP}
51sed '/^#/d' ${TMP} >${OUT}
52
53# Remove trivial code before psu_pll_init_data()
54sed -ni '/psu_pll_init_data/,$p' ${OUT}
55
56# Functions are lowercase in U-Boot, rename them
57sed -i 's/PSU_Mask_Write/psu_mask_write/g' ${OUT}
58sed -i 's/mask_pollOnValue/mask_pollonvalue/g' ${OUT}
59sed -i 's/RegValue/regvalue/g' ${OUT}
60sed -i 's/MaskStatus/maskstatus/g' ${OUT}
61
62sed -i '/&= psu_peripherals_powerdwn_data()/d' ${OUT}
63
64FUNCS_TO_REMOVE="psu_protection
65psu_..._protection
66psu_init_xppu_aper_ram
67mask_delay(u32
68mask_read(u32
69mask_poll(u32
70mask_pollonvalue(u32
71psu_ps_pl_reset_config_data
72psu_ps_pl_isolation_removal_data
73psu_apply_master_tz
74psu_post_config_data
75psu_post_config_data
76psu_peripherals_powerdwn_data
77psu_init_ddr_self_refresh
78xmpu
79xppu
80"
81for i in $FUNCS_TO_REMOVE; do
82sed -i "/$i/,/^}$/d" ${OUT}
83done
84
85scripts/Lindent ${OUT}
86
87# Prepend 'static' to internal functions
88sed -i 's/^.*data(void)$/static &/g' ${OUT}
89sed -i 's/^.*psu_afi_config(void)$/static &/g' ${OUT}
90sed -i 's/^void init_peripheral/static &/g' ${OUT}
91sed -i 's/^int serdes/static &/g' ${OUT}
92sed -i 's/^int init_serdes/static &/g' ${OUT}
93sed -i 's/^unsigned long /static &/g' ${OUT}
94
95sed -i 's/()$/(void)/g' ${OUT}
96sed -i 's/0X/0x/g' ${OUT}
97
98# return (0) -> return 0
99sed -ri 's/return \(([0-9]+)\)/return \1/g' ${OUT}
100
101# Add header
102cat << 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
111EOF
112
113cat ${OUT} >>${TMP}
114cp ${TMP} ${OUT}
115
116# Temporarily convert newlines to do some mangling across lines
117tr "\n" "\r" <${OUT} >${TMP}
118
119# Cleanup empty loops. E.g.:
120# |while (e) {|
121# |           | ==> |while (e)|
122# |    }      |     |    ;    |
123# |           |
124sed -i -r 's| \{\r+(\t*)\}\r\r|\r\1\t;\r|g' ${TMP}
125
126# Remove empty line between variable declaration
127sed -i -r 's|\r(\r\t(unsigned )?int )|\1|g' ${TMP}
128
129# Remove empty lines at function beginning/end
130sed -i -e 's|\r{\r\r|\r{\r|g' ${TMP}
131sed -i -e 's|\r\r}\r|\r}\r|g' ${TMP}
132
133# Remove empty lines after '{' line
134sed -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# | }           |
140sed -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 && ...)
144sed -i -r 's|\((._code .= [x[:xdigit:]]+)\)|\1|g' ${TMP}
145
146# Move helper functions below header includes
147TARGET="#include <xil_io.h>"
148START="static int serdes_rst_seq"
149END="static int serdes_enb_coarse_saturation"
150
151sed -i -e "s|\(${TARGET}\r\r\)\(.*\)\(${START}(.*\)\(${END}(\)|\1\3\2\4|g" \
152    ${TMP}
153
154# Convert back newlines
155tr "\r" "\n" <${TMP} >${OUT}
156
157# Remove unnecessary settings
158# - Low level UART
159SETTINGS_TO_REMOVE="0xFF000000
1600xFF000004
1610xFF000018
1620xFF000034
1630xFF010000
1640xFF010004
1650xFF010018
1660xFF010034
167"
168for i in $SETTINGS_TO_REMOVE; do
169sed -i "/^\tpsu_mask_write($i,.*$/d" ${OUT}
170done
171
172rm ${TMP}
173