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