clksrc-change-registers.awk 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #!/usr/bin/awk -f
  2. #
  3. # Copyright 2010 Ben Dooks <ben-linux@fluff.org>
  4. #
  5. # Released under GPLv2
  6. # example usage
  7. # ./clksrc-change-registers.awk arch/arm/plat-s5pc1xx/include/plat/regs-clock.h < src > dst
  8. function extract_value(s)
  9. {
  10. eqat = index(s, "=")
  11. comat = index(s, ",")
  12. return substr(s, eqat+2, (comat-eqat)-2)
  13. }
  14. function remove_brackets(b)
  15. {
  16. return substr(b, 2, length(b)-2)
  17. }
  18. function splitdefine(l, p)
  19. {
  20. r = split(l, tp)
  21. p[0] = tp[2]
  22. p[1] = remove_brackets(tp[3])
  23. }
  24. function find_length(f)
  25. {
  26. if (0)
  27. printf "find_length " f "\n" > "/dev/stderr"
  28. if (f ~ /0x1/)
  29. return 1
  30. else if (f ~ /0x3/)
  31. return 2
  32. else if (f ~ /0x7/)
  33. return 3
  34. else if (f ~ /0xf/)
  35. return 4
  36. printf "unknown legnth " f "\n" > "/dev/stderr"
  37. exit
  38. }
  39. function find_shift(s)
  40. {
  41. id = index(s, "<")
  42. if (id <= 0) {
  43. printf "cannot find shift " s "\n" > "/dev/stderr"
  44. exit
  45. }
  46. return substr(s, id+2)
  47. }
  48. BEGIN {
  49. if (ARGC < 2) {
  50. print "too few arguments" > "/dev/stderr"
  51. exit
  52. }
  53. # read the header file and find the mask values that we will need
  54. # to replace and create an associative array of values
  55. while (getline line < ARGV[1] > 0) {
  56. if (line ~ /\#define.*_MASK/ &&
  57. !(line ~ /USB_SIG_MASK/)) {
  58. splitdefine(line, fields)
  59. name = fields[0]
  60. if (0)
  61. printf "MASK " line "\n" > "/dev/stderr"
  62. dmask[name,0] = find_length(fields[1])
  63. dmask[name,1] = find_shift(fields[1])
  64. if (0)
  65. printf "=> '" name "' LENGTH=" dmask[name,0] " SHIFT=" dmask[name,1] "\n" > "/dev/stderr"
  66. } else {
  67. }
  68. }
  69. delete ARGV[1]
  70. }
  71. /clksrc_clk.*=.*{/ {
  72. shift=""
  73. mask=""
  74. divshift=""
  75. reg_div=""
  76. reg_src=""
  77. indent=1
  78. print $0
  79. for(; indent >= 1;) {
  80. if ((getline line) <= 0) {
  81. printf "unexpected end of file" > "/dev/stderr"
  82. exit 1;
  83. }
  84. if (line ~ /\.shift/) {
  85. shift = extract_value(line)
  86. } else if (line ~ /\.mask/) {
  87. mask = extract_value(line)
  88. } else if (line ~ /\.reg_divider/) {
  89. reg_div = extract_value(line)
  90. } else if (line ~ /\.reg_source/) {
  91. reg_src = extract_value(line)
  92. } else if (line ~ /\.divider_shift/) {
  93. divshift = extract_value(line)
  94. } else if (line ~ /{/) {
  95. indent++
  96. print line
  97. } else if (line ~ /}/) {
  98. indent--
  99. if (indent == 0) {
  100. if (0) {
  101. printf "shift '" shift "' ='" dmask[shift,0] "'\n" > "/dev/stderr"
  102. printf "mask '" mask "'\n" > "/dev/stderr"
  103. printf "dshft '" divshift "'\n" > "/dev/stderr"
  104. printf "rdiv '" reg_div "'\n" > "/dev/stderr"
  105. printf "rsrc '" reg_src "'\n" > "/dev/stderr"
  106. }
  107. generated = mask
  108. sub(reg_src, reg_div, generated)
  109. if (0) {
  110. printf "/* rsrc " reg_src " */\n"
  111. printf "/* rdiv " reg_div " */\n"
  112. printf "/* shift " shift " */\n"
  113. printf "/* mask " mask " */\n"
  114. printf "/* generated " generated " */\n"
  115. }
  116. if (reg_div != "") {
  117. printf "\t.reg_div = { "
  118. printf ".reg = " reg_div ", "
  119. printf ".shift = " dmask[generated,1] ", "
  120. printf ".size = " dmask[generated,0] ", "
  121. printf "},\n"
  122. }
  123. printf "\t.reg_src = { "
  124. printf ".reg = " reg_src ", "
  125. printf ".shift = " dmask[mask,1] ", "
  126. printf ".size = " dmask[mask,0] ", "
  127. printf "},\n"
  128. }
  129. print line
  130. } else {
  131. print line
  132. }
  133. if (0)
  134. printf indent ":" line "\n" > "/dev/stderr"
  135. }
  136. }
  137. // && ! /clksrc_clk.*=.*{/ { print $0 }