int6kmdio2.c 11 KB

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