mdioblock.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or
  8. * without modification, are permitted (subject to the limitations
  9. * in the disclaimer below) provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials
  18. * provided with the distribution.
  19. *
  20. * * Neither the name of Qualcomm Atheros nor the names of
  21. * its contributors may be used to endorse or promote products
  22. * derived from this software without specific prior written
  23. * permission.
  24. *
  25. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  26. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  27. * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  28. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  31. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  37. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. *--------------------------------------------------------------------*/
  41. /*====================================================================*"
  42. *
  43. * mdioblock.c
  44. *
  45. * Contributor(s):
  46. * Charles Maier
  47. *
  48. *--------------------------------------------------------------------*/
  49. /*====================================================================*
  50. * system header files;
  51. *--------------------------------------------------------------------*/
  52. #include <stdio.h>
  53. #include <ctype.h>
  54. #include <unistd.h>
  55. #include <fcntl.h>
  56. #include <sys/stat.h>
  57. /*====================================================================*
  58. * custom header files;
  59. *--------------------------------------------------------------------*/
  60. #include "../tools/getoptv.h"
  61. #include "../tools/endian.h"
  62. #include "../tools/number.h"
  63. #include "../tools/chars.h"
  64. #include "../tools/error.h"
  65. #include "../tools/flags.h"
  66. #include "../mdio/mdio.h"
  67. /*====================================================================*
  68. * custom source files;
  69. *--------------------------------------------------------------------*/
  70. #ifndef MAKEFILE
  71. #include "../tools/getoptv.c"
  72. #include "../tools/putoptv.c"
  73. #include "../tools/version.c"
  74. #include "../tools/error.c"
  75. #include "../tools/todigit.c"
  76. #endif
  77. /*====================================================================*
  78. * program variables;
  79. *--------------------------------------------------------------------*/
  80. static signed c;
  81. static unsigned row = 1;
  82. static unsigned col = 1;
  83. static uint16_t count = 0;
  84. static uint16_t instr = 0;
  85. static uint16_t addr;
  86. static uint16_t data;
  87. static uint16_t mask;
  88. static uint16_t phy;
  89. static uint16_t reg;
  90. /*====================================================================*
  91. *
  92. * signed mygetc ();
  93. *
  94. * fetch next character from stdin and update the file cursor;
  95. *
  96. *--------------------------------------------------------------------*/
  97. static signed mygetc ()
  98. {
  99. signed c = getc (stdin);
  100. if (c == '\n')
  101. {
  102. row++;
  103. col = 0;
  104. }
  105. else if (~c)
  106. {
  107. col++;
  108. }
  109. return (c);
  110. }
  111. /*====================================================================*
  112. *
  113. * uint16_t integer (unsigned radix);
  114. *
  115. *
  116. *--------------------------------------------------------------------*/
  117. static uint16_t integer (unsigned radix)
  118. {
  119. extern signed c;
  120. uint16_t value = 0;
  121. unsigned digit = 0;
  122. while ((digit = todigit (c)) < radix)
  123. {
  124. value *= radix;
  125. value += digit;
  126. c = mygetc ();
  127. }
  128. while (isspace (c))
  129. {
  130. c = mygetc ();
  131. }
  132. return (value);
  133. }
  134. /*====================================================================*
  135. *
  136. * void assemble (flag_t flags);
  137. *
  138. * read stdin and write stdout; convert hexadecimal input to binary
  139. * MDIO register instructions;
  140. *
  141. * the input file consists of zero or more hexadecimal instructions
  142. * consisting of a phy address, register address, register data and
  143. * register mask; instructions are terminated with semicolon; fields
  144. * are separated by white space; scriptstyle comments are permitted
  145. * between instructions but not between instruction fields;
  146. *
  147. * the output file will consist of one 16-bit program header plus
  148. * one 16-bit MDIO instructions for each input instruction; the
  149. * output is padded to the nearest multiple of 32-bits;
  150. *
  151. *--------------------------------------------------------------------*/
  152. static void assemble (flag_t flags)
  153. {
  154. extern signed c;
  155. c = mygetc ();
  156. while (c != EOF)
  157. {
  158. if (isspace (c))
  159. {
  160. do
  161. {
  162. c = mygetc ();
  163. }
  164. while (isspace (c));
  165. continue;
  166. }
  167. if ((c == '#') || (c == ';'))
  168. {
  169. do
  170. {
  171. c = mygetc ();
  172. }
  173. while (nobreak (c));
  174. continue;
  175. }
  176. phy = integer (16);
  177. reg = integer (16);
  178. data = integer (16);
  179. mask = integer (16);
  180. instr = MDIO16_INSTR (1, 1, phy, reg, 2);
  181. write (STDOUT_FILENO, &instr, sizeof (instr));
  182. data = HTOLE16 (data & 0xFFFF);
  183. write (STDOUT_FILENO, &data, sizeof (data));
  184. mask = HTOLE16 (mask & 0xFFFF);
  185. write (STDOUT_FILENO, &mask, sizeof (mask));
  186. count++;
  187. if (_anyset (flags, MDIO_VERBOSE))
  188. {
  189. fprintf (stderr, "INSTR=0x%04X DATA=0x%04X MASK=0x%04X\n", instr, data, mask);
  190. }
  191. if ((c == ';') || (c == EOF))
  192. {
  193. c = mygetc ();
  194. continue;
  195. }
  196. if (_allclr (flags, MDIO_SILENCE))
  197. {
  198. error (1, 0, "Illegal character or missing terminator: line %d col %d", row, col);
  199. }
  200. }
  201. return;
  202. }
  203. /*====================================================================*
  204. *
  205. * int main (int argc, const char * argv []);
  206. *
  207. *
  208. *
  209. *--------------------------------------------------------------------*/
  210. int main (int argc, const char * argv [])
  211. {
  212. static const char * optv [] =
  213. {
  214. "qv",
  215. "file [ file ] [ ...] > file",
  216. "Atheros Clause 22 MDIO Instruction Block Assembler",
  217. "q\tquiet mode",
  218. "v\tverbose mode",
  219. (const char *) (0)
  220. };
  221. flag_t flags = (flag_t)(0);
  222. optind = 1;
  223. while ((c = getoptv (argc, argv, optv)) != -1)
  224. {
  225. switch (c)
  226. {
  227. case 'q':
  228. _setbits (flags, MDIO_SILENCE);
  229. break;
  230. case 'v':
  231. _setbits (flags, MDIO_VERBOSE);
  232. break;
  233. default:
  234. break;
  235. }
  236. }
  237. argc -= optind;
  238. argv += optind;
  239. if (isatty (STDOUT_FILENO))
  240. {
  241. error (1, ECANCELED, "stdout must be a file or pipe");
  242. }
  243. #if defined (WIN32)
  244. setmode (STDOUT_FILENO, O_BINARY);
  245. #endif
  246. instr = MDIO16_START (1, 0, count);
  247. write (STDOUT_FILENO, &instr, sizeof (instr));
  248. if (!argc)
  249. {
  250. assemble (flags);
  251. }
  252. while ((argc) && (* argv))
  253. {
  254. if (!freopen (* argv, "rb", stdin))
  255. {
  256. error (1, errno, "%s", * argv);
  257. }
  258. assemble (flags);
  259. argc--;
  260. argv++;
  261. }
  262. instr = MDIO16_START (1, 0, count);
  263. addr = count * sizeof (instr) + sizeof (instr);
  264. if ((addr % sizeof (uint32_t)))
  265. {
  266. uint32_t pad = 0;
  267. write (STDOUT_FILENO, &pad, sizeof (pad) - addr % sizeof (pad));
  268. }
  269. if (!lseek (STDOUT_FILENO, 0, SEEK_SET))
  270. {
  271. write (STDOUT_FILENO, &instr, sizeof (instr));
  272. }
  273. if (_anyset (flags, MDIO_VERBOSE))
  274. {
  275. fprintf (stderr, "%d instructions\n", count);
  276. }
  277. return (0);
  278. }