mdioblock2.c.html 9.8 KB

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