mdiogen.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * mdiogen.c -
  11. *
  12. * Contributor(s):
  13. * Charles Maier <cmaier@qca.qualcomm.com>
  14. * Nathaniel Houghton <nhoughto@qca.qualcomm.com>
  15. *
  16. *--------------------------------------------------------------------*/
  17. /*====================================================================*
  18. * system header files;
  19. *--------------------------------------------------------------------*/
  20. #include <stdio.h>
  21. #include <unistd.h>
  22. #include <fcntl.h>
  23. #include <errno.h>
  24. #include <sys/stat.h>
  25. /*====================================================================*
  26. * custom header files;
  27. *--------------------------------------------------------------------*/
  28. #include "../tools/number.h"
  29. #include "../tools/memory.h"
  30. #include "../tools/endian.h"
  31. #include "../tools/error.h"
  32. #include "../mdio/mdio.h"
  33. /*====================================================================*
  34. * program variables;
  35. *--------------------------------------------------------------------*/
  36. const char * program_name;
  37. /*====================================================================*
  38. *
  39. * void regview (void const * memory, uint16_t offset, uint16_t length, FILE *fp);
  40. *
  41. * memory.h
  42. *
  43. * print memory as a binary dump showing absolute memory offsets and
  44. * 16-bit binary register maps; sample output looks like:
  45. *
  46. * ADDR DATA 5432-1098-7654-3210
  47. * 0000 0000 0000-0000-0000-0000
  48. * 0002 0000 0000-0000-0000-0000
  49. * 0004 0000 0000-0000-0000-0000
  50. * 0008 0000 0000-0000-0000-0000
  51. * 0010 0000 0000-0000-0000-0000
  52. *
  53. *--------------------------------------------------------------------*/
  54. static void regview (void const * memory, uint16_t offset, uint16_t extent, FILE * fp)
  55. {
  56. uint16_t * origin = (uint16_t *) (memory);
  57. printf ("ADDR DATA 5432-1098-7654-3210\n");
  58. while (extent >= sizeof (* origin))
  59. {
  60. signed bit = sizeof (* origin) << 3;
  61. printf ("%04u ", offset);
  62. printf ("%04X ", LE16TOH (* origin));
  63. while (bit--)
  64. {
  65. putc (((* origin >> bit) & 1)? '1': '0', fp);
  66. if ((bit) && ! (bit % 4))
  67. {
  68. putc ('-', fp);
  69. }
  70. }
  71. putc ('\n', fp);
  72. offset += sizeof (* origin);
  73. extent -= sizeof (* origin);
  74. origin++;
  75. }
  76. return;
  77. }
  78. /*====================================================================*
  79. *
  80. * int main (int argc, const char * argv []);
  81. *
  82. * this is a basic program that produces an MDIO program block
  83. * using macros defined in mdio.h; the output filename is fixed;
  84. *
  85. * declare your MDIO program by editing array program [] based on
  86. * the MDIO program rules defined in the Atheros PLC Firmware TRM
  87. * under the description of the VS_MOD_OP;
  88. *
  89. *
  90. *--------------------------------------------------------------------*/
  91. #define MDIO_PROGRAM "mdio.bin"
  92. int main (int argc, const char * argv [])
  93. {
  94. uint16_t program [] =
  95. {
  96. #if 1
  97. MDIO16_START (1, 0, 9),
  98. MDIO16_INSTR (1, 1, 0x18, 0x00, 2),
  99. 0x0000,
  100. 0xFFFF,
  101. MDIO16_INSTR (1, 1, 0x10, 0x16, 2),
  102. 0x003F,
  103. 0xFFFF,
  104. MDIO16_INSTR (1, 1, 0x10, 0x17, 2),
  105. 0x7E3F,
  106. 0xFFFF,
  107. MDIO16_INSTR (1, 1, 0x18, 0x00, 2),
  108. 0x0000,
  109. 0xFFFF,
  110. MDIO16_INSTR (1, 1, 0x10, 0x02, 2),
  111. 0x0700,
  112. 0xFFFF,
  113. MDIO16_INSTR (1, 1, 0x10, 0x03, 2),
  114. 0x0000,
  115. 0xFFFF,
  116. MDIO16_INSTR (1, 1, 0x18, 0x00, 2),
  117. 0x0000,
  118. 0xFFFF,
  119. MDIO16_INSTR (1, 1, 0x14, 0x00, 2),
  120. 0x007D,
  121. 0xFFFF,
  122. MDIO16_INSTR (1, 1, 0x14, 0x01, 2),
  123. 0x0000,
  124. 0xFFFF,
  125. #else
  126. MDIO16_START (1, 0, 24),
  127. MDIO32_INSTR (0x2c, 0x7e3f003f, 0xffffffff),
  128. MDIO32_INSTR (0x8, 0x500, 0xffffffff),
  129. MDIO32_INSTR (0x100, 0x7d, 0xffffffff),
  130. MDIO32_INSTR (0x30, 0x19f005f2, 0xffffffff),
  131. MDIO32_INSTR (0x200, 0x7d, 0xffffffff),
  132. MDIO32_INSTR (0x300, 0x7d, 0xffffffff),
  133. MDIO32_INSTR (0x400, 0x7d, 0xffffffff),
  134. MDIO32_INSTR (0x500, 0x7d, 0xffffffff),
  135. #endif
  136. };
  137. signed fd;
  138. program_name = * argv;
  139. if (-- argc)
  140. {
  141. error (1, ECANCELED, "Too many command line arguments");
  142. }
  143. regview (& program, 0, sizeof (program), stdout);
  144. if ((fd = open (MDIO_PROGRAM, O_CREAT | O_WRONLY | O_TRUNC, S_IRWXU | S_IRGRP | S_IROTH)) == - 1)
  145. {
  146. error (1, errno, "Can't open %s", MDIO_PROGRAM);
  147. }
  148. if (write (fd, program, sizeof (program)) != sizeof (program))
  149. {
  150. unlink (MDIO_PROGRAM);
  151. error (1, errno, "Can't write %s", MDIO_PROGRAM);
  152. }
  153. if (sizeof (program) % sizeof (uint32_t))
  154. {
  155. uint32_t zeros = 0;
  156. ssize_t count = sizeof (uint32_t) - sizeof (program) % sizeof (uint32_t);
  157. if (write (fd, & zeros, count) != count)
  158. {
  159. unlink (MDIO_PROGRAM);
  160. error (1, errno, "Can't write %s", MDIO_PROGRAM);
  161. }
  162. }
  163. close (fd);
  164. return (0);
  165. }