blob: 384bb56e1a165342bb97c1ebdc57b277ad8de70d [file] [log] [blame]
Luca Ceresoli84a2c832019-05-24 15:40:02 +02001#!/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
67dpll_prog
68mask_poll(u32
69mask_pollonvalue(u32
70psu_ps_pl_reset_config_data
71psu_ps_pl_isolation_removal_data
72psu_apply_master_tz
73psu_post_config_data
74psu_post_config_data
75psu_peripherals_powerdwn_data
76psu_init_ddr_self_refresh
77xmpu
78xppu
79"
80for i in $FUNCS_TO_REMOVE; do
81sed -i "/$i/,/^}$/d" ${OUT}
82done
83
84scripts/Lindent ${OUT}
85
86# Prepend 'static' to internal functions
87sed -i 's/^.*data(void)$/static &/g' ${OUT}
88sed -i 's/^.*psu_afi_config(void)$/static &/g' ${OUT}
89sed -i 's/^void init_peripheral/static &/g' ${OUT}
90sed -i 's/^int serdes/static &/g' ${OUT}
91sed -i 's/^int init_serdes/static &/g' ${OUT}
92sed -i 's/^unsigned long /static &/g' ${OUT}
93
94sed -i 's/()$/(void)/g' ${OUT}
95sed -i 's/0X/0x/g' ${OUT}
96
Luca Ceresoli7f492f32019-06-11 18:39:40 +020097# return (0) -> return 0
98sed -ri 's/return \(([0-9]+)\)/return \1/g' ${OUT}
99
Luca Ceresoli84a2c832019-05-24 15:40:02 +0200100# Add header
101cat << EOF >${TMP}
102// SPDX-License-Identifier: GPL-2.0+
103/*
104 * (c) Copyright 2015 Xilinx, Inc. All rights reserved.
105 */
106
107#include <asm/arch/psu_init_gpl.h>
108#include <xil_io.h>
109
110EOF
111
112cat ${OUT} >>${TMP}
113cp ${TMP} ${OUT}
114
115# Temporarily convert newlines to do some mangling across lines
116tr "\n" "\r" <${OUT} >${TMP}
117
118# Cleanup empty loops. E.g.:
119# |while (e) {|
120# | | ==> |while (e)|
121# | } | | ; |
122# | |
123sed -i -r 's| \{\r+(\t*)\}\r\r|\n\1\t;\n|g' ${TMP}
124
125# Remove empty line between variable declaration
126sed -i -r 's|\r(\r\t(unsigned )?int )|\1|g' ${TMP}
127
128# Remove empty lines at function beginning/end
129sed -i -e 's|\r{\r\r|\r{\r|g' ${TMP}
130sed -i -e 's|\r\r}\r|\r}\r|g' ${TMP}
131
132# Remove empty lines after '{' line
133sed -i -e 's| {\r\r| {\r|g' ${TMP}
134
135# Remove braces {} around single statement blocks. E.g.:
136# | while (e) { | | while (e) |
137# | stg(); | => | stg();|
138# | } |
139sed -i -r 's| \{(\r[^\r]*;)\r\t*\}|\1|g' ${TMP}
140
141# Remove Unnecessary parentheses around 'n_code <= 0x3C' and similar. E.g.:
142# if ((p_code >= 0x26) && ...) -> if (p_code >= 0x26 && ...)
143sed -i -r 's|\((._code .= [x[:xdigit:]]+)\)|\1|g' ${TMP}
144
145# Convert back newlines
146tr "\r" "\n" <${TMP} >${OUT}
147
148rm ${TMP}