mdioblock.c.html 9.0 KB

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