zynqmp_psu_init_minimize.sh 4.3 KB

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