genpintab.awk 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. #!/usr/bin/gawk -f
  2. BEGIN{
  3. pad_index=0;
  4. sig_index=0;
  5. FS=",";
  6. ret=0;
  7. if(length(head)==0)
  8. headfile="cat 1>include/mach/gpio_data.h";
  9. else
  10. headfile="cat 1>" head;
  11. warning_on=0;
  12. print "/* this file is automatic generate . Please do not edit it"
  13. print " ./genpintab.awk gpio_pinmux.csv > <this file name> can generate this file*/"
  14. print "#define AOBUS_REG_ADDR_MASK(a) AOBUS_REG_ADDR(((a)&0xffff))"
  15. }
  16. #function conflict(name,value)
  17. #{
  18. # if(length(table[name])==0)
  19. # {
  20. # table[name]=value;
  21. # return 0;
  22. # }
  23. # if( table[name]!=value)
  24. # {
  25. # print "POINT D error found conflict name=" name " value=" value | "cat 1>&2" ;
  26. # print "origin value=" table[name] " NR=" $2| "cat 1>&2" ;
  27. # return 1;
  28. # }
  29. # return 0;
  30. #
  31. #}
  32. /^TITLE/{
  33. for(j=1;j<=NF;j++){
  34. n=split($j,a," ");
  35. if(n==0)
  36. continue;
  37. column[j]=a[1];
  38. for(i=2;i<=n;i++)
  39. {
  40. column[i]= column[i] " " a[i];
  41. }
  42. }
  43. }
  44. /^MODULE/{
  45. for(j=1;j<=NF;j++){
  46. n=split($j,a," ");
  47. if(n==0)
  48. continue;
  49. module[j]=a[1];
  50. for(i=2;i<=n;i++)
  51. {
  52. module[i]= module[i] "_" a[i];
  53. }
  54. }
  55. }
  56. /^REG_ADDR/{
  57. for(i=1;i<=NF;i++)
  58. {
  59. str=$i;
  60. delete a;
  61. n=split(str,a," ");
  62. if(n!=2&&n!=1)
  63. continue;
  64. if(n==2){
  65. reg_addr[a[1]]=a[2];
  66. reg_addr_isnum[a[1]]=0;
  67. print "#define " a[1] " (" a[2] ")"
  68. }
  69. else{
  70. reg_addr[a[1]]=a[1];
  71. reg_addr_isnum[a[1]]=1;
  72. }
  73. }
  74. }
  75. /^ALIAS/{
  76. k=0;
  77. for(i=1;i<=NF;i++)
  78. {
  79. str=$i;
  80. delete a;
  81. n=split(str,a," ");
  82. if(n==4 || n==5)
  83. {
  84. if(length(idx[a[2]])==0)
  85. {
  86. idx[a[2]]=0;
  87. }
  88. }
  89. else
  90. continue;
  91. if(n == 4)
  92. {
  93. reg_list[a[2],idx[a[2]]]=a[2]"_" a[3] ;
  94. reg_alias[a[1]]=a[2] "(" (idx[a[2]]);
  95. print "#define " reg_list[a[2],idx[a[2]]] " " a[4] "(" a[1] ")"
  96. idx[a[2]]++;
  97. }else if (n == 5){
  98. for(j=1;j<=strtonum(a[4]);j++)
  99. {
  100. if(reg_addr_isnum[a[1]])
  101. ind=sprintf("0x%x",strtonum(a[1])+(j-1)*strtonum(a[3]));
  102. else{
  103. ind=sprintf("%s%d",a[1],(j-1));
  104. print "#define " ind " (" a[1] "+" (j-1)")"
  105. }
  106. reg_list[a[2],idx[a[2]]]=a[2]"_" (j-1) ;
  107. # print reg_list[a[2],0]
  108. reg_alias[ind]=a[2] "(" (idx[a[2]]);
  109. print "#define " reg_list[a[2],idx[a[2]]] " " a[5] "(" ind ")"
  110. idx[a[2]]++;
  111. }
  112. }else{
  113. continue;
  114. }
  115. }
  116. }
  117. /^PAD/{
  118. row[NR]=$2;
  119. if(length(pads[$2])!=0)
  120. {
  121. print "found pad name conflict name=" $2 "line=" NR| "cat 1>&2" ;
  122. print "origin value=" pads[$2] | "cat 1>&2" ;
  123. exit 1
  124. }
  125. pads[$2]=pad_index++;
  126. for(i=3;i<=5;i++){
  127. n=length($i);
  128. if(n==0)
  129. continue;
  130. delete a;
  131. str=$i;
  132. n=split(str,a," ");
  133. if(n!=2)
  134. {
  135. print "POINT A error found at NR=" NR "NF=" $i | "cat 1>&2" ;
  136. exit 1
  137. }
  138. if(length(reg_alias[a[1]])==0)
  139. {
  140. print "use original name for reg=_" a[1] | "cat 1>&2"
  141. reg_alias[a[1]]="_" a[1];
  142. }
  143. if(match(a[2],/[\(|\[]([[:digit:]]+)[\)|\]]/,array) == 0)
  144. {
  145. print "POINT B error found at pad=" $2 " column=" column[i] " value=" $i | "cat 1>&2" ;
  146. exit 1
  147. }
  148. pads_gpio[$2,column[i]] = reg_alias[a[1]] "," array[1] ")";
  149. }
  150. str="";
  151. for(i=6;i<=NF;i++)
  152. {
  153. n=length($i);
  154. if(n==0)
  155. continue;
  156. delete a;
  157. n=split($i,a," ");
  158. sig=null;
  159. for(j=1;j<=n;j++){
  160. delete temp_arr;
  161. enable="xxx";
  162. n_reg=0;
  163. if(a[j] ~ /^N_([[:alpha:]_][[:alnum:]_]*)[\(|\[]([[:digit:]]+)[\)|\]]$/)
  164. {
  165. str=substr(a[j],3);
  166. enable="disable";
  167. }else if(a[j] ~ /([[:alpha:]_][[:alnum:]_]*)[\(|\[]([[:digit:]]+)[\)|\]]$/)
  168. {
  169. str=a[j];
  170. enable="enable";
  171. }else if( a[j] ~ /^[a-zA-Z_][a-zA-Z0-9_]*$/ )
  172. {
  173. sig=a[j];
  174. if(length(pads_pinmux[$2,sig,"enable"]) != 0 ||length(pads_pinmux[$2,sig,"disable"])!= 0)
  175. {
  176. if (length(module[i])!=0)
  177. {
  178. if(length(pads_pinmux[$2,sig,"enable"]) != 0)
  179. enable="enable";
  180. else
  181. enable="disable";
  182. if(warning_on>=1)print "Waring found, pads conflict " | "cat 1>&2"
  183. if(warning_on>=1)print "\tsig=" sig " pads_pinmux[" $2 "," sig "," enable "]=" pads_pinmux[$2,sig,enable] | "cat 1>&2"
  184. sig=a[j] "_" module[i];
  185. if(warning_on>=1)print "\treplace name is " sig | "cat 1>&2"
  186. }
  187. else {
  188. if(length(pads_pinmux[$2,sig,"enable"]) != 0)
  189. enable="enable";
  190. else
  191. enable="disable";
  192. print "error found, pads conflict " | "cat 1>&2"
  193. print "\tsig=" sig " pads_pinmux[" $2 "," sig "," enable "]=" pads_pinmux[$2,sig,enable] | "cat 1>&2"
  194. ret=1;
  195. break;
  196. }
  197. }
  198. continue;
  199. }
  200. n_reg=match(str,/([[:alpha:]_][[:alnum:]_]*)[\(|\[]([[:digit:]]+)[\)|\]]$/,temp_arr);
  201. if( length(sig)==0 || n_reg==0 || length(temp_arr[2])==0)
  202. {
  203. print "++++error found , wrong format at pad=" $2 " sig=" sig "," column[i] " value=" $i "," i "," n_reg "," temp_arr[2] | "cat 1>&2" ;
  204. ret=1;
  205. break;
  206. }
  207. if( length(pads_pinmux[$2,sig,enable] ) != 0 )
  208. {
  209. print "fatal error found, pads conflict " | "cat 1>&2"
  210. print "\tsig=" sig " pads_pinmux[" $2 "," sig "," enable "]=" pads_pinmux[$2,sig,enable] | "cat 1>&2"
  211. ret=1;
  212. break;
  213. }
  214. if(length(reg_alias[temp_arr[1]])==0){
  215. str=temp_arr[1] "(" temp_arr[2] ")" ;
  216. reg_alias[temp_arr[1]]=temp_arr[1] ;
  217. print "use original name for reg=" temp_arr[1] | "cat 1>&2"
  218. }
  219. else
  220. str=reg_alias[temp_arr[1]] "," temp_arr[2] ")" ;
  221. pads_pinmux[$2,sig,enable]=str;
  222. if(sig in sigs)
  223. continue;
  224. sigs[sig]=sig_index++;
  225. }
  226. }
  227. }
  228. END{
  229. for(i in idx)
  230. {
  231. print "#define " i "(base,bit) (bit+(base<<5))"
  232. print "#define " i "_NUM " "(sizeof(" tolower(i) "_addr)/sizeof(" tolower(i) "_addr[0]))"
  233. print "static unsigned " tolower(i) "_addr[]={"
  234. for(j=0;j<idx[i];j++)
  235. print "\t" reg_list[i,j] ","
  236. print "};"
  237. }
  238. print "#ifndef __MACH_HEAD_GPIO_DATA__" | headfile
  239. print "#define __MACH_HEAD_GPIO_DATA__" | headfile
  240. print "typedef enum {" | headfile
  241. x=0;
  242. for(i in pads)
  243. {
  244. if(x!=0)
  245. printf ",\n" | headfile;
  246. printf ("\tPAD_%s=%d",i,pads[i]) | headfile;
  247. if(i in sigs){
  248. print "pad name " i " conflict" | "cat 1>&2" ;
  249. exit 1;
  250. }
  251. x++;
  252. }
  253. print ",\n\tPAD_MAX_PADS=" x "\n}pad_t;" | headfile
  254. print "typedef enum {" | headfile
  255. x=0;
  256. for(i in sigs)
  257. {
  258. if(x!=0)
  259. printf (",\n") | headfile;
  260. printf ("\tSIG_%s=%d",i,sigs[i]) | headfile;
  261. if(i in pads){
  262. print "sig name " i " conflict" | "cat 1>&2" ;
  263. exit 1;
  264. }
  265. x++;
  266. }
  267. print ",\n\tSIG_GPIOIN=" x++ | headfile
  268. print ",\n\tSIG_GPIOOUT=" x++ "," | headfile
  269. print "\tSIG_MAX_SIGS=" x "\n}sig_t;" | headfile
  270. print "#endif /*__MACH_HEAD_GPIO_DATA__*/" | headfile
  271. print "#define NOT_EXIST -1"
  272. print "struct pad_sig {pad_t pad;sig_t sig;unsigned enable; unsigned disable;};"
  273. print "#define foreach_pad_sig_start(pad,sig) {int __i;for(__i=0;__i<sizeof(pad_sig_tab)/sizeof(pad_sig_tab[0]);__i++){ unsigned __pad=pad,__sig=sig; "
  274. print "#define case_pad_equal(enable,disable) if(pad_sig_tab[__i].pad==__pad&&pad_sig_tab[__i].sig!=__sig){ enable=pad_sig_tab[__i].enable;disable=pad_sig_tab[__i].disable"
  275. print "#define case_sig_equal(enable,disable) if(pad_sig_tab[__i].pad!=__pad&&pad_sig_tab[__i].sig==__sig){enable=pad_sig_tab[__i].enable;disable=pad_sig_tab[__i].disable"
  276. print "#define case_both_equal(enable,disable) if(pad_sig_tab[__i].pad==__pad&&pad_sig_tab[__i].sig==__sig){enable=pad_sig_tab[__i].enable;disable=pad_sig_tab[__i].disable"
  277. print "#define case_end };"
  278. print "#define foreach_pad_sig_end };}"
  279. print "static struct pad_sig pad_sig_tab[]={"
  280. for( combine in pads_pinmux )
  281. {
  282. split(combine,dim,SUBSEP)
  283. pad=dim[1];sig=dim[2];enable=dim[3];
  284. if(enable == "disable")
  285. {
  286. if(length ( pads_pinmux[pad,sig,"enable"] )!=0)
  287. continue;
  288. enable=NOT_EXIST;
  289. disable=pads_pinmux[pad,sig,"disable"];
  290. }else{
  291. enable=pads_pinmux[pad,sig,"enable"];
  292. disable="NOT_EXIST";
  293. if(length ( pads_pinmux[pad,sig,"disable"] )!=0)
  294. {
  295. disable=pads_pinmux[pad,sig,"disable"];
  296. }
  297. }
  298. printf "\t"
  299. print "{.pad=PAD_" pad ",.sig=SIG_" sig ",.enable=" enable \
  300. ",.disable=" disable "},"
  301. }
  302. print "};"
  303. print "static const char * pad_name[]={"
  304. x=0;
  305. for(i in pads)
  306. {
  307. printf "\t[%d]=\"%s\",\n",pads[i],i;
  308. if(i in sigs){
  309. print "pad name " i " conflict" | "cat 1>&2" ;
  310. exit 1;
  311. }
  312. x++;
  313. }
  314. print "\t[PAD_MAX_PADS]=NULL\n};"
  315. print "static const char * sig_name[]={"
  316. x=0;
  317. for(i in sigs)
  318. {
  319. printf "\t[%d]=\"%s\",\n",sigs[i],i;
  320. if(i in pads){
  321. print "sig name " i " conflict" | "cat 1>&2" ;
  322. exit 1;
  323. }
  324. x++;
  325. }
  326. print "\t[SIG_GPIOIN]=\"GPIOIN\","
  327. print "\t[SIG_GPIOOUT]=\"GPIOOUT\","
  328. print "\t[SIG_MAX_SIGS]=NULL\n};"
  329. print "/* GPIO operation part */"
  330. x=0;
  331. print "static unsigned pad_gpio_bit[]={"
  332. for( combine in pads_gpio )
  333. {
  334. split(combine,dim,SUBSEP)
  335. pad=dim[1];sig=dim[2];
  336. if(sig != column[3])
  337. {
  338. continue;
  339. };
  340. if(x!=0)
  341. printf ",\n";
  342. printf "\t[PAD_%s]=%s",pad,pads_gpio[pad,sig];
  343. x++;
  344. }
  345. print "\n};"
  346. exit ret
  347. }