plcmdio32.c.html 13 KB

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