int6kmdio2.c.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. int6kmdio2.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='int6klog.c.html' title=' int6klog.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='int6kmdio.c.html' title=' int6kmdio.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. /*====================================================================*
  62. *
  63. * int6kmdio2 - Qualcomm Atheros 32-bit MDIO Register Editor
  64. *
  65. *
  66. * Contributor(s):
  67. * Nathaniel Houghton &lt;nhoughto@qca.qualcomm.com&gt;
  68. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  69. *
  70. *--------------------------------------------------------------------*/
  71. /*====================================================================*&quot;
  72. * system header files;
  73. *--------------------------------------------------------------------*/
  74. #include &lt;unistd.h&gt;
  75. #include &lt;stdlib.h&gt;
  76. #include &lt;string.h&gt;
  77. #include &lt;ctype.h&gt;
  78. /*====================================================================*
  79. * custom header files;
  80. *--------------------------------------------------------------------*/
  81. #include &quot;../tools/getoptv.h&quot;
  82. #include &quot;../tools/putoptv.h&quot;
  83. #include &quot;../tools/memory.h&quot;
  84. #include &quot;../tools/number.h&quot;
  85. #include &quot;../tools/symbol.h&quot;
  86. #include &quot;../tools/types.h&quot;
  87. #include &quot;../tools/flags.h&quot;
  88. #include &quot;../tools/files.h&quot;
  89. #include &quot;../tools/error.h&quot;
  90. #include &quot;../plc/plc.h&quot;
  91. #include &quot;../ram/nvram.h&quot;
  92. #include &quot;../ram/sdram.h&quot;
  93. #include &quot;../nvm/nvm.h&quot;
  94. #include &quot;../pib/pib.h&quot;
  95. #include &quot;../mme/mme.h&quot;
  96. #include &quot;../mdio/mdio.h&quot;
  97. /*====================================================================*
  98. * custom source files;
  99. *--------------------------------------------------------------------*/
  100. #ifndef MAKEFILE
  101. #include &quot;../plc/Devices.c&quot;
  102. #include &quot;../plc/Display.c&quot;
  103. #include &quot;../mme/UnwantedMessage.c&quot;
  104. #endif
  105. #ifndef MAKEFILE
  106. #include &quot;../tools/getoptv.c&quot;
  107. #include &quot;../tools/putoptv.c&quot;
  108. #include &quot;../tools/version.c&quot;
  109. #include &quot;../tools/uintspec.c&quot;
  110. #include &quot;../tools/hexencode.c&quot;
  111. #include &quot;../tools/hexdecode.c&quot;
  112. #include &quot;../tools/todigit.c&quot;
  113. #include &quot;../tools/hexdump.c&quot;
  114. #include &quot;../tools/hexview.c&quot;
  115. #include &quot;../tools/regview32.c&quot;
  116. #include &quot;../tools/synonym.c&quot;
  117. #include &quot;../tools/error.c&quot;
  118. #endif
  119. #ifndef MAKEFILE
  120. #include &quot;../ether/channel.c&quot;
  121. #include &quot;../ether/openchannel.c&quot;
  122. #include &quot;../ether/closechannel.c&quot;
  123. #include &quot;../ether/readpacket.c&quot;
  124. #include &quot;../ether/sendpacket.c&quot;
  125. #endif
  126. #ifndef MAKEFILE
  127. #include &quot;../mme/EthernetHeader.c&quot;
  128. #include &quot;../mme/QualcommHeader.c&quot;
  129. #include &quot;../mme/MMECode.c&quot;
  130. #endif
  131. /*====================================================================*
  132. * program constants;
  133. *--------------------------------------------------------------------*/
  134. #define MDIO_FLAG_REVERSE (1 &lt;&lt; 0)
  135. #define MDIO_MODE_READ 0
  136. #define MDIO_MODE_WRITE 1
  137. #define CODE_SHIFT 3
  138. #define CODE_MASK (0x03 &lt;&lt; CODE_SHIFT)
  139. #define CODE_HIGH_ADDR 0x03
  140. #define CODE_LOW_ADDR 0x02
  141. #define HIGH_ADDR_SHIFT 9
  142. #define LOW_ADDR_SHIFT 1
  143. #define HIGH_ADDR_MASK (0x000003FF &lt;&lt; HIGH_ADDR_SHIFT)
  144. #define LOW_ADDR_MASK 0x000001FC
  145. /*====================================================================*
  146. *
  147. * signed mdio (struct channel * channel, uint8_t mode, uint8_t phy, uint16_t * data);
  148. *
  149. *
  150. *
  151. *--------------------------------------------------------------------*/
  152. static signed mdio (struct channel * channel, uint8_t mode, uint8_t phy, uint8_t reg, uint16_t * data)
  153. {
  154. struct message message;
  155. signed packetsize;
  156. #ifndef __GNUC__
  157. #pragma pack (push,1)
  158. #endif
  159. struct __packed vs_mdio_command_request
  160. {
  161. struct ethernet_hdr ethernet;
  162. struct qualcomm_hdr qualcomm;
  163. uint8_t OPERATION;
  164. uint8_t PHY;
  165. uint8_t REG;
  166. uint16_t DATA;
  167. }
  168. * request = (struct vs_mdio_command_request *)(&amp;message);
  169. struct __packed vs_mdio_command_confirm
  170. {
  171. struct ethernet_hdr ethernet;
  172. struct qualcomm_hdr qualcomm;
  173. uint8_t MSTATUS;
  174. uint16_t DATA;
  175. uint8_t PHY;
  176. uint8_t REG;
  177. }
  178. * confirm = (struct vs_mdio_command_confirm *)(&amp;message);
  179. #ifndef __GNUC__
  180. #pragma pack (pop)
  181. #endif
  182. memset (&amp;message, 0, sizeof (message));
  183. EthernetHeader (&amp;request-&gt;ethernet, channel-&gt;peer, channel-&gt;host, channel-&gt;type);
  184. QualcommHeader (&amp;request-&gt;qualcomm, 0, (VS_MDIO_COMMAND | MMTYPE_REQ));
  185. request-&gt;OPERATION = mode;
  186. request-&gt;PHY = phy;
  187. request-&gt;REG = reg;
  188. request-&gt;DATA = HTOLE16 (*data);
  189. #if 1
  190. printf (&quot; phy 0x%02X&quot;, phy);
  191. printf (&quot; reg 0x%02X&quot;, reg);
  192. printf (&quot; data 0x%04X&quot;, * data);
  193. printf (&quot;\n&quot;);
  194. #endif
  195. if (sendpacket (channel, &amp;message, (ETHER_MIN_LEN - ETHER_CRC_LEN)) == -1)
  196. {
  197. error (1, errno, CHANNEL_CANTSEND);
  198. }
  199. while ((packetsize = readpacket (channel, &amp;message, sizeof (message))) &gt; 0)
  200. {
  201. if (UnwantedMessage (&amp;message, packetsize, 0, (VS_MDIO_COMMAND | MMTYPE_CNF)))
  202. {
  203. continue;
  204. }
  205. if (confirm-&gt;MSTATUS)
  206. {
  207. error (0, 0, &quot;%s (%0X): %s&quot;, MMECode (confirm-&gt;qualcomm.MMTYPE, confirm-&gt;MSTATUS), confirm-&gt;MSTATUS, PLC_WONTDOIT);
  208. continue;
  209. }
  210. *data = confirm-&gt;DATA;
  211. return (0);
  212. }
  213. return (-1);
  214. }
  215. /*====================================================================*
  216. *
  217. * void function (struct channel * channel, uint8_t mode, uint32_t address, uint32_t content, flag_t flags);
  218. *
  219. *
  220. *
  221. *--------------------------------------------------------------------*/
  222. static void function (struct channel * channel, uint8_t mode, uint32_t address, uint32_t content, flag_t flags)
  223. {
  224. uint8_t phy;
  225. uint8_t reg;
  226. uint16_t mdio_data;
  227. uint16_t high_addr = (address &amp; HIGH_ADDR_MASK) &gt;&gt; HIGH_ADDR_SHIFT;
  228. uint16_t low_addr = (address &amp; LOW_ADDR_MASK) &gt;&gt; LOW_ADDR_SHIFT;
  229. /*
  230. * supply chip with high address bytes
  231. */
  232. phy = CODE_HIGH_ADDR &lt;&lt; CODE_SHIFT;
  233. reg = 0;
  234. mdio_data = high_addr;
  235. if (mdio (channel, MDIO_MODE_WRITE, phy, reg, &amp;mdio_data))
  236. {
  237. error (1, 0, &quot;could not set high address bits&quot;);
  238. }
  239. if (_allclr (flags, MDIO_FLAG_REVERSE))
  240. {
  241. /*
  242. * supply chip with first low address bytes and first data chunk
  243. */
  244. phy = CODE_LOW_ADDR &lt;&lt; CODE_SHIFT;
  245. phy |= (low_addr &amp; 0xE0) &gt;&gt; 5;
  246. reg = (low_addr &amp; 0x1F);
  247. mdio_data = (content &amp; 0x0000FFFF);
  248. if (mdio (channel, mode, phy, reg, &amp;mdio_data))
  249. {
  250. error (1, 0, &quot;could not read low 16bits&quot;);
  251. }
  252. if (mode == MDIO_MODE_READ)
  253. {
  254. content = mdio_data;
  255. }
  256. /*
  257. * supply chip with second low address bytes and second data chunk
  258. */
  259. phy = CODE_LOW_ADDR &lt;&lt; CODE_SHIFT;
  260. phy |= (low_addr &amp; 0xE0) &gt;&gt; 5;
  261. reg = (low_addr &amp; 0x1F) | 0x01;
  262. mdio_data = (content &amp; 0xFFFF0000) &gt;&gt; 16;
  263. if (mdio (channel, mode, phy, reg, &amp;mdio_data))
  264. {
  265. error (1, 0, &quot;could not read high 16bits&quot;);
  266. }
  267. if (mode == MDIO_MODE_READ)
  268. {
  269. content |= (mdio_data &lt;&lt; 16);
  270. printf (&quot;0x%08x: 0x%08x\n&quot;, address, content);
  271. }
  272. }
  273. else
  274. {
  275. /*
  276. * supply chip with second low address bytes and second data chunk
  277. */
  278. phy = CODE_LOW_ADDR &lt;&lt; CODE_SHIFT;
  279. phy |= (low_addr &amp; 0xE0) &gt;&gt; 5;
  280. reg = (low_addr &amp; 0x1F) | 0x01;
  281. mdio_data = (content &amp; 0xFFFF0000) &gt;&gt; 16;
  282. if (mdio (channel, mode, phy, reg, &amp;mdio_data))
  283. {
  284. error (1, 0, &quot;could not read high 16bits&quot;);
  285. }
  286. if (mode == MDIO_MODE_READ)
  287. {
  288. content = (mdio_data &lt;&lt; 16);
  289. }
  290. /*
  291. * supply chip with first low address bytes and first data chunk
  292. */
  293. phy = CODE_LOW_ADDR &lt;&lt; CODE_SHIFT;
  294. phy |= (low_addr &amp; 0xE0) &gt;&gt; 5;
  295. reg = (low_addr &amp; 0x1F);
  296. mdio_data = (content &amp; 0x0000FFFF);
  297. if (mdio (channel, mode, phy, reg, &amp;mdio_data))
  298. {
  299. error (1, 0, &quot;could not read low 16bits&quot;);
  300. }
  301. if (mode == MDIO_MODE_READ)
  302. {
  303. content |= mdio_data;
  304. printf (&quot;0x%08x: 0x%08x\n&quot;, address, content);
  305. }
  306. }
  307. return;
  308. }
  309. /*====================================================================*
  310. *
  311. * int main (int argc, char const * argv []);
  312. *
  313. *
  314. *
  315. *--------------------------------------------------------------------*/
  316. int main (int argc, char const * argv [])
  317. {
  318. extern struct channel channel;
  319. static char const * optv [] =
  320. {
  321. &quot;a:d:hei:qv&quot;,
  322. &quot;[device] [...]&quot;,
  323. &quot;Qualcomm Atheros 32-bit MDIO Register Editor&quot;,
  324. &quot;a n\taddress is (n) [0x00000000]&quot;,
  325. &quot;d n\tcontent is (n) [0x00000000]&quot;,
  326. &quot;e\tredirect stderr to stdout&quot;,
  327. &quot;h\tsend high-order data before low-order data&quot;,
  328. #if defined (WINPCAP) || defined (LIBPCAP)
  329. &quot;i n\thost interface is (n) [&quot; LITERAL (CHANNEL_ETHNUMBER) &quot;]&quot;,
  330. #else
  331. &quot;i s\thost interface is (s) [&quot; LITERAL (CHANNEL_ETHDEVICE) &quot;]&quot;,
  332. #endif
  333. &quot;q\tquiet mode&quot;,
  334. &quot;v\tverbose mode&quot;,
  335. (char const *) (0)
  336. };
  337. flag_t flags = (flag_t)(0);
  338. uint8_t mode = MDIO_MODE_READ;
  339. uint32_t address = 0;
  340. uint32_t content = 0;
  341. signed c;
  342. if (getenv (PLCDEVICE))
  343. {
  344. #if defined (WINPCAP) || defined (LIBPCAP)
  345. channel.ifindex = atoi (getenv (PLCDEVICE));
  346. #else
  347. channel.ifname = strdup (getenv (PLCDEVICE));
  348. #endif
  349. }
  350. optind = 1;
  351. while ((c = getoptv (argc, argv, optv)) != -1)
  352. {
  353. switch (c)
  354. {
  355. case 'a':
  356. address = (uint32_t)(uintspec (optarg, 0, 0x0007FFFF));
  357. if (address &amp; 0x03)
  358. {
  359. error (1, 0, &quot;address must be on an even 4 byte boundary&quot;);
  360. }
  361. break;
  362. case 'd':
  363. mode = MDIO_MODE_WRITE;
  364. content = (uint32_t)(uintspec (optarg, 0, 0xFFFFFFFF));
  365. break;
  366. case 'e':
  367. dup2 (STDOUT_FILENO, STDERR_FILENO);
  368. break;
  369. case 'i':
  370. #if defined (WINPCAP) || defined (LIBPCAP)
  371. channel.ifindex = atoi (optarg);
  372. #else
  373. channel.ifname = optarg;
  374. #endif
  375. break;
  376. case 'q':
  377. _setbits (channel.flags, CHANNEL_SILENCE);
  378. break;
  379. case 'r':
  380. _setbits (flags, MDIO_FLAG_REVERSE);
  381. break;
  382. case 'v':
  383. _setbits (channel.flags, CHANNEL_VERBOSE);
  384. break;
  385. default:
  386. break;
  387. }
  388. }
  389. argc -= optind;
  390. argv += optind;
  391. openchannel (&amp;channel);
  392. if (!argc)
  393. {
  394. function (&amp;channel, mode, address, content, flags);
  395. }
  396. while ((argc) &amp;&amp; (* argv))
  397. {
  398. if (!hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  399. {
  400. error (1, errno, PLC_BAD_MAC, * argv);
  401. }
  402. function (&amp;channel, mode, address, content, flags);
  403. argv++;
  404. argc--;
  405. }
  406. closechannel (&amp;channel);
  407. return (0);
  408. }
  409. </pre>
  410. <div class='footerlink'>
  411. [<a href='int6klog.c.html' title=' int6klog.c '>PREV</a>]
  412. [<a href='toolkit.html' title=' Index '>HOME</a>]
  413. [<a href='int6kmdio.c.html' title=' int6kmdio.c '>NEXT</a>]
  414. </div>
  415. </body>
  416. </html>