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 |
| 67 | dpll_prog |
| 68 | mask_poll(u32 |
| 69 | mask_pollonvalue(u32 |
| 70 | psu_ps_pl_reset_config_data |
| 71 | psu_ps_pl_isolation_removal_data |
| 72 | psu_apply_master_tz |
| 73 | psu_post_config_data |
| 74 | psu_post_config_data |
| 75 | psu_peripherals_powerdwn_data |
| 76 | psu_init_ddr_self_refresh |
| 77 | xmpu |
| 78 | xppu |
| 79 | " |
| 80 | for i in $FUNCS_TO_REMOVE; do |
| 81 | sed -i "/$i/,/^}$/d" ${OUT} |
| 82 | done |
| 83 | |
| 84 | scripts/Lindent ${OUT} |
| 85 | |
| 86 | # Prepend 'static' to internal functions |
| 87 | sed -i 's/^.*data(void)$/static &/g' ${OUT} |
| 88 | sed -i 's/^.*psu_afi_config(void)$/static &/g' ${OUT} |
| 89 | sed -i 's/^void init_peripheral/static &/g' ${OUT} |
| 90 | sed -i 's/^int serdes/static &/g' ${OUT} |
| 91 | sed -i 's/^int init_serdes/static &/g' ${OUT} |
| 92 | sed -i 's/^unsigned long /static &/g' ${OUT} |
| 93 | |
| 94 | sed -i 's/()$/(void)/g' ${OUT} |
| 95 | sed -i 's/0X/0x/g' ${OUT} |
| 96 | |
Luca Ceresoli | 7f492f3 | 2019-06-11 18:39:40 +0200 | [diff] [blame] | 97 | # return (0) -> return 0 |
| 98 | sed -ri 's/return \(([0-9]+)\)/return \1/g' ${OUT} |
| 99 | |
Luca Ceresoli | 84a2c83 | 2019-05-24 15:40:02 +0200 | [diff] [blame] | 100 | # Add header |
| 101 | cat << 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 | |
| 110 | EOF |
| 111 | |
| 112 | cat ${OUT} >>${TMP} |
| 113 | cp ${TMP} ${OUT} |
| 114 | |
| 115 | # Temporarily convert newlines to do some mangling across lines |
| 116 | tr "\n" "\r" <${OUT} >${TMP} |
| 117 | |
| 118 | # Cleanup empty loops. E.g.: |
| 119 | # |while (e) {| |
| 120 | # | | ==> |while (e)| |
| 121 | # | } | | ; | |
| 122 | # | | |
| 123 | sed -i -r 's| \{\r+(\t*)\}\r\r|\n\1\t;\n|g' ${TMP} |
| 124 | |
| 125 | # Remove empty line between variable declaration |
| 126 | sed -i -r 's|\r(\r\t(unsigned )?int )|\1|g' ${TMP} |
| 127 | |
| 128 | # Remove empty lines at function beginning/end |
| 129 | sed -i -e 's|\r{\r\r|\r{\r|g' ${TMP} |
| 130 | sed -i -e 's|\r\r}\r|\r}\r|g' ${TMP} |
| 131 | |
| 132 | # Remove empty lines after '{' line |
| 133 | sed -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 | # | } | |
| 139 | sed -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 && ...) |
| 143 | sed -i -r 's|\((._code .= [x[:xdigit:]]+)\)|\1|g' ${TMP} |
| 144 | |
| 145 | # Convert back newlines |
| 146 | tr "\r" "\n" <${TMP} >${OUT} |
| 147 | |
| 148 | rm ${TMP} |