mdioblock2.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. * mdioblock2.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 uint32_t addr;
  86. static uint32_t data;
  87. static uint32_t mask;
  88. /*====================================================================*
  89. *
  90. * signed mygetc ();
  91. *
  92. * return next input character after updating the cursor position;
  93. *
  94. *--------------------------------------------------------------------*/
  95. static signed mygetc ()
  96. {
  97. extern unsigned row;
  98. extern unsigned col;
  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. * uint32_t integer (unsigned radix);
  114. *
  115. *
  116. *--------------------------------------------------------------------*/
  117. static uint32_t integer (unsigned radix)
  118. {
  119. uint32_t value = 0;
  120. unsigned digit = 0;
  121. while ((digit = todigit (c)) < radix)
  122. {
  123. value *= radix;
  124. value += digit;
  125. c = mygetc ();
  126. }
  127. while (isspace (c))
  128. {
  129. c = mygetc ();
  130. }
  131. return (value);
  132. }
  133. /*====================================================================*
  134. *
  135. * void assemble (flag_t flags);
  136. *
  137. * read stdin and write stdout; convert hexadecimal input to binary
  138. * MDIO register instructions;
  139. *
  140. * the input file consists of zero or more hexadecimal instructions
  141. * consisting of register address, register data and register mask;
  142. * instructions are terminated with semicolon; fields are separated
  143. * by white space; scriptstyle comments are permitted between
  144. * instructions but not between instruction fields;
  145. *
  146. * the output file will consist of one 16-bit program header plus
  147. * nine 16-bit MDIO instructions for each input instruction; the
  148. * output is padded to the nearest multiple of 32-bits;
  149. *
  150. *--------------------------------------------------------------------*/
  151. static void assemble (flag_t flags)
  152. {
  153. c = mygetc ();
  154. while (c != EOF)
  155. {
  156. if (isspace (c))
  157. {
  158. do
  159. {
  160. c = mygetc ();
  161. }
  162. while (isspace (c));
  163. continue;
  164. }
  165. if ((c == '#') || (c == ';'))
  166. {
  167. do
  168. {
  169. c = mygetc ();
  170. }
  171. while (nobreak (c));
  172. continue;
  173. }
  174. addr = integer (16);
  175. data = integer (16);
  176. mask = integer (16);
  177. instr = MDIO16_INSTR (1, 1, (0x03 << 3), 0, 2);
  178. write (STDOUT_FILENO, &instr, sizeof (instr));
  179. instr = HTOLE16 ((addr >> 9) & 0x03FF);
  180. write (STDOUT_FILENO, &instr, sizeof (instr));
  181. instr = HTOLE16 (0xFFFF);
  182. write (STDOUT_FILENO, &instr, sizeof (instr));
  183. count++;
  184. instr = MDIO16_INSTR (1, 1, (0x02 << 3) | ((MDIO32_LO_ADDR (addr) & 0xE0) >> 5), MDIO32_LO_ADDR (addr) & 0x1F, 2);
  185. write (STDOUT_FILENO, &instr, sizeof (instr));
  186. instr = HTOLE16 (data & 0xFFFF);
  187. write (STDOUT_FILENO, &instr, sizeof (instr));
  188. instr = HTOLE16 (mask & 0xFFFF);
  189. write (STDOUT_FILENO, &instr, sizeof (instr));
  190. count++;
  191. instr = MDIO16_INSTR (1, 1, (0x02 << 3) | ((MDIO32_LO_ADDR (addr) & 0xE0) >> 5), (MDIO32_LO_ADDR (addr) & 0x1F) | 0x01, 2);
  192. write (STDOUT_FILENO, &instr, sizeof (instr));
  193. instr = HTOLE16 ((data >> 16) & 0xFFFF);
  194. write (STDOUT_FILENO, &instr, sizeof (instr));
  195. instr = HTOLE16 ((mask >> 16) & 0xFFFF);
  196. write (STDOUT_FILENO, &instr, sizeof (instr));
  197. count++;
  198. if (_anyset (flags, MDIO_VERBOSE))
  199. {
  200. fprintf (stderr, "REG=0x%08X DATA=0x%08X MASK=0x%08X\n", addr, data, mask);
  201. }
  202. if ((c == ';') || (c == EOF))
  203. {
  204. c = mygetc ();
  205. continue;
  206. }
  207. if (_allclr (flags, MDIO_SILENCE))
  208. {
  209. error (1, 0, "Illegal character or missing terminator: line %d col %d", row, col);
  210. }
  211. }
  212. return;
  213. }
  214. /*====================================================================*
  215. *
  216. * int main (int argc, const char * argv []);
  217. *
  218. *
  219. *
  220. *--------------------------------------------------------------------*/
  221. int main (int argc, const char * argv [])
  222. {
  223. static const char * optv [] =
  224. {
  225. "qv",
  226. "file [ file ] [ ...] > file",
  227. "Atheros Clause 45 MDIO Instruction Block Assembler",
  228. "q\tquiet mode",
  229. "v\tverbose mode",
  230. (const char *) (0)
  231. };
  232. flag_t flags = (flag_t)(0);
  233. optind = 1;
  234. while ((c = getoptv (argc, argv, optv)) != -1)
  235. {
  236. switch (c)
  237. {
  238. case 'q':
  239. _setbits (flags, MDIO_SILENCE);
  240. break;
  241. case 'v':
  242. _setbits (flags, MDIO_VERBOSE);
  243. break;
  244. default:
  245. break;
  246. }
  247. }
  248. argc -= optind;
  249. argv += optind;
  250. if (isatty (STDOUT_FILENO))
  251. {
  252. error (1, ECANCELED, "stdout must be a file or pipe");
  253. }
  254. #if defined (WIN32)
  255. setmode (STDOUT_FILENO, O_BINARY);
  256. #endif
  257. instr = MDIO16_START (1, 0, count);
  258. write (STDOUT_FILENO, &instr, sizeof (instr));
  259. if (!argc)
  260. {
  261. assemble (flags);
  262. }
  263. while ((argc) && (* argv))
  264. {
  265. if (!freopen (* argv, "rb", stdin))
  266. {
  267. error (1, errno, "%s", * argv);
  268. }
  269. assemble (flags);
  270. argc--;
  271. argv++;
  272. }
  273. instr = MDIO16_START (1, 0, count);
  274. addr = count * sizeof (instr) + sizeof (instr);
  275. if ((addr % sizeof (uint32_t)))
  276. {
  277. uint32_t pad = 0;
  278. write (STDOUT_FILENO, &pad, sizeof (pad) - addr % sizeof (pad));
  279. }
  280. if (!lseek (STDOUT_FILENO, 0, SEEK_SET))
  281. {
  282. write (STDOUT_FILENO, &instr, sizeof (instr));
  283. }
  284. if (_anyset (flags, MDIO_VERBOSE))
  285. {
  286. fprintf (stderr, "%d instructions\n", count);
  287. }
  288. return (0);
  289. }