mdiogen.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. * mdiogen.c -
  44. *
  45. *
  46. * Contributor(s):
  47. * Charles Maier
  48. * Nathaniel Houghton
  49. *
  50. *--------------------------------------------------------------------*/
  51. /*====================================================================*
  52. * system header files;
  53. *--------------------------------------------------------------------*/
  54. #include <stdio.h>
  55. #include <unistd.h>
  56. #include <fcntl.h>
  57. #include <errno.h>
  58. #include <sys/stat.h>
  59. /*====================================================================*
  60. * custom header files;
  61. *--------------------------------------------------------------------*/
  62. #include "../tools/number.h"
  63. #include "../tools/memory.h"
  64. #include "../tools/endian.h"
  65. #include "../tools/error.h"
  66. #include "../tools/files.h"
  67. #include "../mdio/mdio.h"
  68. /*====================================================================*
  69. * custom source files;
  70. *--------------------------------------------------------------------*/
  71. #ifndef MAKEFILE
  72. #include "../tools/error.c"
  73. #endif
  74. /*====================================================================*
  75. * program variables;
  76. *--------------------------------------------------------------------*/
  77. const char * program_name;
  78. /*====================================================================*
  79. *
  80. * void regview (void const * memory, uint16_t offset, uint16_t length, FILE *fp);
  81. *
  82. * memory.h
  83. *
  84. * print memory as a binary dump showing absolute memory offsets and
  85. * 16-bit binary register maps; sample output looks like:
  86. *
  87. * ADDR DATA 5432-1098-7654-3210
  88. * 0000 0000 0000-0000-0000-0000
  89. * 0002 0000 0000-0000-0000-0000
  90. * 0004 0000 0000-0000-0000-0000
  91. * 0008 0000 0000-0000-0000-0000
  92. * 0010 0000 0000-0000-0000-0000
  93. *
  94. *
  95. *--------------------------------------------------------------------*/
  96. static void regview (void const * memory, uint16_t offset, uint16_t extent, FILE *fp)
  97. {
  98. uint16_t * origin = (uint16_t *)(memory);
  99. printf ("ADDR DATA 5432-1098-7654-3210\n");
  100. while (extent >= sizeof (* origin))
  101. {
  102. signed bit = sizeof (* origin) << 3;
  103. printf ("%04u ", offset);
  104. printf ("%04X ", LE16TOH (*origin));
  105. while (bit--)
  106. {
  107. putc (((* origin >> bit) & 1)? '1': '0', fp);
  108. if ((bit) && !(bit%4))
  109. {
  110. putc ('-', fp);
  111. }
  112. }
  113. putc ('\n', fp);
  114. offset += sizeof (* origin);
  115. extent -= sizeof (* origin);
  116. origin++;
  117. }
  118. return;
  119. }
  120. /*====================================================================*
  121. *
  122. * int main (int argc, const char * argv []);
  123. *
  124. * this is a basic program that produces an MDIO program block
  125. * using macros defined in mdio.h; the output filename is fixed;
  126. *
  127. * declare your MDIO program by editing array program [] based on
  128. * the MDIO program rules defined in the Atheros PLC Firmware TRM
  129. * under the description of the VS_MOD_OP;
  130. *
  131. *
  132. *--------------------------------------------------------------------*/
  133. #define MDIO_PROGRAM "mdio.bin"
  134. int main (int argc, const char * argv [])
  135. {
  136. uint16_t program [] =
  137. {
  138. #if 1
  139. MDIO16_START (1, 0, 9),
  140. MDIO16_INSTR (1, 1, 0x18, 0x00, 2),
  141. 0x0000,
  142. 0xFFFF,
  143. MDIO16_INSTR (1, 1, 0x10, 0x16, 2),
  144. 0x003F,
  145. 0xFFFF,
  146. MDIO16_INSTR (1, 1, 0x10, 0x17, 2),
  147. 0x7E3F,
  148. 0xFFFF,
  149. MDIO16_INSTR (1, 1, 0x18, 0x00, 2),
  150. 0x0000,
  151. 0xFFFF,
  152. MDIO16_INSTR (1, 1, 0x10, 0x02, 2),
  153. 0x0700,
  154. 0xFFFF,
  155. MDIO16_INSTR (1, 1, 0x10, 0x03, 2),
  156. 0x0000,
  157. 0xFFFF,
  158. MDIO16_INSTR (1, 1, 0x18, 0x00, 2),
  159. 0x0000,
  160. 0xFFFF,
  161. MDIO16_INSTR (1, 1, 0x14, 0x00, 2),
  162. 0x007D,
  163. 0xFFFF,
  164. MDIO16_INSTR (1, 1, 0x14, 0x01, 2),
  165. 0x0000,
  166. 0xFFFF,
  167. #else
  168. MDIO16_START (1, 0, 24),
  169. MDIO32_INSTR (0x2c, 0x7e3f003f, 0xffffffff),
  170. MDIO32_INSTR (0x8, 0x500, 0xffffffff),
  171. MDIO32_INSTR (0x100, 0x7d, 0xffffffff),
  172. MDIO32_INSTR (0x30, 0x19f005f2, 0xffffffff),
  173. MDIO32_INSTR (0x200, 0x7d, 0xffffffff),
  174. MDIO32_INSTR (0x300, 0x7d, 0xffffffff),
  175. MDIO32_INSTR (0x400, 0x7d, 0xffffffff),
  176. MDIO32_INSTR (0x500, 0x7d, 0xffffffff),
  177. #endif
  178. };
  179. signed fd;
  180. program_name = * argv;
  181. if (--argc)
  182. {
  183. error (1, ECANCELED, "Too many command line arguments");
  184. }
  185. regview (&program, 0, sizeof (program), stdout);
  186. if ((fd = open (MDIO_PROGRAM, O_CREAT|O_WRONLY|O_TRUNC, FILE_FILEMODE)) == -1)
  187. {
  188. error (1, errno, "Can't open %s", MDIO_PROGRAM);
  189. }
  190. if (write (fd, program, sizeof (program)) != sizeof (program))
  191. {
  192. unlink (MDIO_PROGRAM);
  193. error (1, errno, "Can't write %s", MDIO_PROGRAM);
  194. }
  195. if (sizeof (program) % sizeof (uint32_t))
  196. {
  197. uint32_t zeros = 0;
  198. ssize_t count = sizeof (uint32_t) - sizeof (program) % sizeof (uint32_t);
  199. if (write (fd, &zeros, count) != count)
  200. {
  201. unlink (MDIO_PROGRAM);
  202. error (1, errno, "Can't write %s", MDIO_PROGRAM);
  203. }
  204. }
  205. close (fd);
  206. return (0);
  207. }