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