mdioblock.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * mdioblock.c
  11. *
  12. * Contributor(s):
  13. * Charles Maier <cmaier@qca.qualcomm.com>
  14. *
  15. *--------------------------------------------------------------------*/
  16. /*====================================================================*
  17. * system header files;
  18. *--------------------------------------------------------------------*/
  19. #include <stdio.h>
  20. #include <ctype.h>
  21. #include <unistd.h>
  22. #include <fcntl.h>
  23. #include <sys/stat.h>
  24. /*====================================================================*
  25. * custom header files;
  26. *--------------------------------------------------------------------*/
  27. #include "../tools/getoptv.h"
  28. #include "../tools/endian.h"
  29. #include "../tools/number.h"
  30. #include "../tools/chars.h"
  31. #include "../tools/error.h"
  32. #include "../tools/flags.h"
  33. #include "../mdio/mdio.h"
  34. /*====================================================================*
  35. * custom source files;
  36. *--------------------------------------------------------------------*/
  37. #ifndef MAKEFILE
  38. #include "../tools/getoptv.c"
  39. #include "../tools/putoptv.c"
  40. #include "../tools/version.c"
  41. #include "../tools/error.c"
  42. #include "../tools/todigit.c"
  43. #endif
  44. /*====================================================================*
  45. * program variables;
  46. *--------------------------------------------------------------------*/
  47. static signed c;
  48. static unsigned row = 1;
  49. static unsigned col = 1;
  50. static uint16_t count = 0;
  51. static uint16_t instr = 0;
  52. static uint16_t addr;
  53. static uint16_t data;
  54. static uint16_t mask;
  55. static uint16_t phy;
  56. static uint16_t reg;
  57. /*====================================================================*
  58. *
  59. * signed mygetc ();
  60. *
  61. * fetch next character from stdin and update the file cursor;
  62. *
  63. *--------------------------------------------------------------------*/
  64. static signed mygetc ()
  65. {
  66. signed c = getc (stdin);
  67. if (c == '\n')
  68. {
  69. row++;
  70. col = 0;
  71. }
  72. else if (~ c)
  73. {
  74. col++;
  75. }
  76. return (c);
  77. }
  78. /*====================================================================*
  79. *
  80. * uint16_t integer (unsigned radix);
  81. *
  82. *--------------------------------------------------------------------*/
  83. static uint16_t integer (unsigned radix)
  84. {
  85. extern signed c;
  86. uint16_t value = 0;
  87. unsigned digit = 0;
  88. while ((digit = todigit (c)) < radix)
  89. {
  90. value *= radix;
  91. value += digit;
  92. c = mygetc ();
  93. }
  94. while (isspace (c))
  95. {
  96. c = mygetc ();
  97. }
  98. return (value);
  99. }
  100. /*====================================================================*
  101. *
  102. * void assemble (flag_t flags);
  103. *
  104. * read stdin and write stdout; convert hexadecimal input to binary
  105. * MDIO register instructions;
  106. *
  107. * the input file consists of zero or more hexadecimal instructions
  108. * consisting of a phy address, register address, register data and
  109. * register mask; instructions are terminated with semicolon; fields
  110. * are separated by white space; scriptstyle comments are permitted
  111. * between instructions but not between instruction fields;
  112. *
  113. * the output file will consist of one 16-bit program header plus
  114. * one 16-bit MDIO instructions for each input instruction; the
  115. * output is padded to the nearest multiple of 32-bits;
  116. *
  117. *--------------------------------------------------------------------*/
  118. static void assemble (flag_t flags)
  119. {
  120. extern signed c;
  121. c = mygetc ();
  122. while (c != EOF)
  123. {
  124. if (isspace (c))
  125. {
  126. do
  127. {
  128. c = mygetc ();
  129. }
  130. while (isspace (c));
  131. continue;
  132. }
  133. if ((c == '#') || (c == ';'))
  134. {
  135. do
  136. {
  137. c = mygetc ();
  138. }
  139. while (nobreak (c));
  140. continue;
  141. }
  142. phy = integer (16);
  143. reg = integer (16);
  144. data = integer (16);
  145. mask = integer (16);
  146. instr = MDIO16_INSTR (1, 1, phy, reg, 2);
  147. write (STDOUT_FILENO, & instr, sizeof (instr));
  148. data = HTOLE16 (data & 0xFFFF);
  149. write (STDOUT_FILENO, & data, sizeof (data));
  150. mask = HTOLE16 (mask & 0xFFFF);
  151. write (STDOUT_FILENO, & mask, sizeof (mask));
  152. count++;
  153. if (_anyset (flags, MDIO_VERBOSE))
  154. {
  155. fprintf (stderr, "INSTR=0x%04X DATA=0x%04X MASK=0x%04X\n", instr, data, mask);
  156. }
  157. if ((c == ';') || (c == EOF))
  158. {
  159. c = mygetc ();
  160. continue;
  161. }
  162. if (_allclr (flags, MDIO_SILENCE))
  163. {
  164. error (1, 0, "Illegal character or missing terminator: line %d col %d", row, col);
  165. }
  166. }
  167. return;
  168. }
  169. /*====================================================================*
  170. *
  171. * int main (int argc, const char * argv []);
  172. *
  173. *
  174. *--------------------------------------------------------------------*/
  175. int main (int argc, const char * argv [])
  176. {
  177. static const char * optv [] =
  178. {
  179. "qv",
  180. "file [ file ] [ ...] > file",
  181. "Qualcomm Atheros Clause 22 MDIO Instruction Block Assembler",
  182. "q\tquiet mode",
  183. "v\tverbose mode",
  184. (const char *) (0)
  185. };
  186. flag_t flags = (flag_t) (0);
  187. optind = 1;
  188. while (~ (c = getoptv (argc, argv, optv)))
  189. {
  190. switch (c)
  191. {
  192. case 'q':
  193. _setbits (flags, MDIO_SILENCE);
  194. break;
  195. case 'v':
  196. _setbits (flags, MDIO_VERBOSE);
  197. break;
  198. default:
  199. break;
  200. }
  201. }
  202. argc -= optind;
  203. argv += optind;
  204. if (isatty (STDOUT_FILENO))
  205. {
  206. error (1, ECANCELED, "stdout must be a file or pipe");
  207. }
  208. #if defined (WIN32)
  209. setmode (STDOUT_FILENO, O_BINARY);
  210. #endif
  211. instr = MDIO16_START (1, 0, count);
  212. write (STDOUT_FILENO, & instr, sizeof (instr));
  213. if (! argc)
  214. {
  215. assemble (flags);
  216. }
  217. while ((argc) && (* argv))
  218. {
  219. if (! freopen (* argv, "rb", stdin))
  220. {
  221. error (1, errno, "%s", * argv);
  222. }
  223. assemble (flags);
  224. argc--;
  225. argv++;
  226. }
  227. instr = MDIO16_START (1, 0, count);
  228. addr = count * sizeof (instr) + sizeof (instr);
  229. if ((addr % sizeof (uint32_t)))
  230. {
  231. uint32_t pad = 0;
  232. write (STDOUT_FILENO, & pad, sizeof (pad) - addr % sizeof (pad));
  233. }
  234. if (! lseek (STDOUT_FILENO, 0, SEEK_SET))
  235. {
  236. write (STDOUT_FILENO, & instr, sizeof (instr));
  237. }
  238. if (_anyset (flags, MDIO_VERBOSE))
  239. {
  240. fprintf (stderr, "%d instructions\n", count);
  241. }
  242. return (0);
  243. }