zynqmp_psu_init_minimize.sh 3.7 KB

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