plcmdio32.c 11 KB

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