123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741 |
- <?xml version='1.0' encoding='iso-8859-1'?>
- <!doctype html public '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
- <html xmlns='http://www.w3c.org/1999/xhtml' lang='en-us'>
- <head>
- <title>
- mdiodump.c
- </title>
- <meta http-equiv='content-type' content='text/html;iso-8859-1'/>
- <meta name='generator' content='motley-tools 1.9.4 13:40:33 Feb 18 2015'/>
- <meta name='author' content='cmaier@cmassoc.net'/>
- <meta name='robots' content='noindex,nofollow'/>
- <link href='toolkit.css' rel='stylesheet' type='text/css'/>
- </head>
- <body>
- <div class='headerlink'>
- [<a href='mdioblock.c.html' title=' mdioblock.c '>PREV</a>]
- [<a href='toolkit.html' title=' Index '>HOME</a>]
- [<a href='mdiogen.c.html' title=' mdiogen.c '>NEXT</a>]
- </div>
- <pre>
- /*====================================================================*
- *
- * Copyright (c) 2013 Qualcomm Atheros, Inc.
- *
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or
- * without modification, are permitted (subject to the limitations
- * in the disclaimer below) provided that the following conditions
- * are met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following
- * disclaimer in the documentation and/or other materials
- * provided with the distribution.
- *
- * * Neither the name of Qualcomm Atheros nor the names of
- * its contributors may be used to endorse or promote products
- * derived from this software without specific prior written
- * permission.
- *
- * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
- * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
- * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
- * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
- * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- *--------------------------------------------------------------------*/
- /*====================================================================*
- *
- * mdiodump.c - Atheros MDIO Custom Module Analyser
- *
- * Contributor(s):
- * Nathaniel Houghton <nhoughto@qca.qualcomm.com>
- * Charles Maier <cmaier@qca.qualcomm.com>
- * Marc Bertola <mbertola@qti.qualcomm.com>
- *
- *--------------------------------------------------------------------*/
- #define _GETOPT_H
- /*====================================================================*
- * system header files;
- *--------------------------------------------------------------------*/
- #include <errno.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- /*====================================================================*
- * custom header files;
- *--------------------------------------------------------------------*/
- #include "../tools/getoptv.h"
- #include "../tools/flags.h"
- #include "../tools/error.h"
- #include "../tools/files.h"
- #include "../tools/endian.h"
- #include "../tools/symbol.h"
- /*====================================================================*
- * custom source files;
- *--------------------------------------------------------------------*/
- #ifndef MAKEFILE
- #include "../tools/getoptv.c"
- #include "../tools/putoptv.c"
- #include "../tools/version.c"
- #include "../tools/hexstring.c"
- #include "../tools/hexdecode.c"
- #include "../tools/codelist.c"
- #include "../tools/error.c"
- #include "../tools/lookup.c"
- #include "../tools/assist.c"
- #endif
- /*====================================================================*
- * program constants;
- *--------------------------------------------------------------------*/
- #define MDIODUMP_SUMMARY (1 << 0)
- #define MDIODUMP_VERBOSE (1 << 1)
- #define MDIO32_NORMAL 0x00
- #define MDIO32_ACCESS_USING_HIGH 0x02
- #define MDIO32_SET_HIGH 0x03
- /*====================================================================*
- * supported PHY types;
- *--------------------------------------------------------------------*/
- #define PHY_GENERIC 0
- #define PHY_AR8236 1
- struct _code_ switches [] =
- {
- {
- PHY_GENERIC,
- "generic"
- },
- {
- PHY_AR8236,
- "ar8236"
- }
- };
- /*====================================================================*
- * command structure;
- *--------------------------------------------------------------------*/
- #ifndef __GNUC__
- #pragma pack (push,1)
- #endif
- struct __packed command
- {
- uint16_t ctrl;
- uint16_t data;
- uint16_t mask;
- };
- #ifndef __GNUC__
- #pragma pack (pop)
- #endif
- /*====================================================================*
- * register & memmap struction
- *--------------------------------------------------------------------*/
- #define PHY_REGISTER 0
- #define GBL_REGISTER 1
- struct reg
- {
- uint32_t address;
- uint8_t phy;
- uint8_t reg;
- uint32_t content;
- uint8_t type;
- };
- struct memmap
- {
- unsigned size;
- unsigned used;
- struct reg * reg;
- };
- /*====================================================================*
- *
- * signed write_phy_reg (struct memmap * memmap, uint8_t phy, uint8_t reg, uint16_t data, uint16_t mask);
- *
- *
- *
- *--------------------------------------------------------------------*/
- static signed write_phy_reg (struct memmap * memmap, uint8_t phy, uint8_t reg, uint16_t data, uint16_t mask)
- {
- unsigned i;
- for (i = 0; i < memmap->used; ++i)
- {
- if (memmap->reg [i].type != PHY_REGISTER)
- {
- continue;
- }
- if (memmap->reg [i].phy != phy)
- {
- continue;
- }
- if (memmap->reg [i].reg == reg)
- {
- continue;
- }
- memmap->reg [i].content &= mask;
- memmap->reg [i].content |= mask & data;
- return (0);
- }
- if (memmap->used < memmap->size)
- {
- memmap->reg [i].phy = phy;
- memmap->reg [i].reg = reg;
- memmap->reg [i].content = mask & data;
- memmap->reg [i].type = PHY_REGISTER;
- memmap->used++;
- return (0);
- }
- error (1, 0, "not enough registers to run simulation");
- return (-1);
- }
- /*====================================================================*
- *
- * signed write_gbl_reg (struct memmap *memmap, uint32_t address, uint8_t upper, uint16_t data, uint16_t mask);
- *
- *
- *
- *--------------------------------------------------------------------*/
- static signed write_gbl_reg (struct memmap * memmap, uint32_t address, uint8_t upper, uint16_t data, uint16_t mask)
- {
- unsigned i;
- for (i = 0; i < memmap->used; ++i)
- {
- if (memmap->reg [i].type != GBL_REGISTER)
- {
- continue;
- }
- if (memmap->reg [i].address != address)
- {
- continue;
- }
- if (upper)
- {
- memmap->reg [i].content &= (mask << 16) | 0x0000FFFF;
- memmap->reg [i].content |= (mask & data) << 16;
- }
- else
- {
- memmap->reg [i].content &= mask | 0xFFFF0000;
- memmap->reg [i].content |= mask & data;
- }
- return (0);
- }
- if (memmap->used < memmap->size)
- {
- memmap->reg [i].address = address;
- memmap->reg [i].content = mask & data;
- if (upper)
- {
- memmap->reg [i].content <<= 16;
- }
- memmap->reg [i].type = GBL_REGISTER;
- memmap->used++;
- return (0);
- }
- error (1, 0, "not enough registers to run simulation");
- return (-1);
- }
- #if 0
- /*====================================================================*
- *
- * signed read_phy_reg (struct memmap * memmap, uint8_t phy, uint8_t reg, uint32_t * data);
- *
- *
- *
- *--------------------------------------------------------------------*/
- static signed read_phy_reg (struct memmap * memmap, uint8_t phy, uint8_t reg, uint32_t * data)
- {
- unsigned i;
- for (i = 0; i < memmap->used; ++i)
- {
- if (memmap->reg [i].type != PHY_REGISTER)
- {
- continue;
- }
- if (memmap->reg [i].phy != phy)
- {
- continue;
- }
- if (memmap->reg [i].reg != reg)
- {
- continue;
- }
- *data = memmap->reg [i].content;
- return (0);
- }
- return (-1);
- }
- /*====================================================================*
- *
- * signed read_gbl_reg (struct memmap * memmap, uint32_t address, uint32_t * content);
- *
- *
- *
- *--------------------------------------------------------------------*/
- static signed read_gbl_reg (struct memmap * memmap, uint32_t address, uint32_t * content)
- {
- unsigned i;
- for (i = 0; i < memmap->used; ++i)
- {
- if (memmap->reg [i].type != GBL_REGISTER)
- {
- continue;
- }
- if (memmap->reg [i].address != address)
- {
- continue;
- }
- * content = memmap->reg [i].content;
- return (0);
- }
- return (-1);
- }
- #endif
- /*====================================================================*
- *
- * void print_memmap (struct memmap *memmap);
- *
- *
- *
- *--------------------------------------------------------------------*/
- static void print_memmap (struct memmap * memmap)
- {
- unsigned i;
- for (i = 0; i < memmap->used; ++i)
- {
- if (memmap->reg [i].type == PHY_REGISTER)
- {
- printf ("phy 0x%02x, reg 0x%02x: 0x%04x\n", memmap->reg [i].phy, memmap->reg [i].reg, memmap->reg [i].content);
- }
- if (memmap->reg [i].type == GBL_REGISTER)
- {
- printf ("0x%08x: 0x%08x\n", memmap->reg [i].address, memmap->reg [i].content);
- }
- }
- return;
- }
- /*====================================================================*
- *
- * void print_command (struct command *command);
- *
- *
- *
- *--------------------------------------------------------------------*/
- static void print_command (struct command * command)
- {
- union __packed
- {
- uint16_t data;
- struct __packed
- {
- uint16_t start: 2;
- uint16_t operation: 2;
- uint16_t phy_address: 5;
- uint16_t reg_address: 5;
- uint16_t turnaround: 2;
- }
- bits;
- }
- ctrl;
- ctrl.data = command->ctrl;
- printf ("%02x %02x %04x %04x;\n", ctrl.bits.phy_address, ctrl.bits.reg_address, command->data, command->mask);
- return;
- }
- /*====================================================================*
- *
- * signed init_memmap (unsigned count, struct memmap * memmap);
- *
- *
- *
- *--------------------------------------------------------------------*/
- static signed init_memmap (unsigned count, struct memmap * memmap)
- {
- memmap->reg = calloc (count, sizeof (struct reg));
- if (memmap->reg == NULL)
- {
- error (1, errno, "could not allocate reg memory");
- }
- memmap->size = count;
- memmap->used = 0;
- return (0);
- }
- /*====================================================================*
- *
- * void free_memmap (struct memmap * memmap);
- *
- *
- *
- *--------------------------------------------------------------------*/
- static void free_memmap (struct memmap * memmap)
- {
- free (memmap->reg);
- return;
- }
- /*====================================================================*
- *
- * signed phy_ar8236 (char const * filename, unsigned commands, flag_t flags);
- *
- *
- *--------------------------------------------------------------------*/
- static signed phy_ar8236 (char const * filename, unsigned commands, flag_t flags)
- {
- struct command command;
- struct memmap memmap;
- signed ar8236_code;
- signed set_high_addr = 0;
- uint16_t high_addr = 0;
- uint32_t address;
- uint16_t low_address;
- if (init_memmap (commands, &memmap))
- {
- error (1, 0, "could not allocate memory for simulation");
- }
- while (commands--)
- {
- if (read (STDIN_FILENO, &command, sizeof (struct command)) != sizeof (struct command))
- {
- error (0, errno, FILE_CANTREAD, filename);
- return (-1);
- }
- command.ctrl = LE16TOH (command.ctrl);
- command.data = LE16TOH (command.data);
- command.mask = LE16TOH (command.mask);
- ar8236_code = (command.ctrl & 0x180) >> 7;
- switch (ar8236_code)
- {
- case MDIO32_NORMAL:
- if (_anyset (flags, MDIODUMP_VERBOSE))
- {
- printf ("Normal MDIO Operation:\n");
- printf ("\tPhy Address: 0x%02x\n", (command.ctrl & 0x1F0) >> 4);
- printf ("\tRegister Address: 0x%02x\n", (command.ctrl & 0x3E00) >> 9);
- }
- if ((command.ctrl & 0x0C) >> 2 == 0x01)
- {
- write_phy_reg (&memmap, (command.ctrl & 0x1F0) >> 4, (command.ctrl & 0x3E00) >> 9, command.data, command.mask);
- }
- break;
- case MDIO32_SET_HIGH:
- set_high_addr = 1;
- high_addr = command.data & 0x3FF & command.mask;
- if ((command.ctrl & 0x0C) >> 2 == 0x01)
- {
- if (_anyset (flags, MDIODUMP_VERBOSE))
- {
- printf ("Set High Address to 0x%03x:\n", high_addr);
- }
- }
- else
- {
- if (_anyset (flags, MDIODUMP_VERBOSE))
- {
- printf ("Read High Address:\n");
- }
- }
- break;
- case MDIO32_ACCESS_USING_HIGH:
- if (!set_high_addr)
- {
- error (0, 0, "warning: high address bits not set when attempting to do a 32 bit read, assuming high address bits are 0");
- high_addr = 0;
- }
- low_address = (command.ctrl & 0x3E00) >> 9;
- low_address |= (command.ctrl & 0x070) << 1;
- address = high_addr << 9;
- address |= (low_address << 1) & 0xFFFFFFFC;
- if (low_address & 0x01)
- {
- if (_anyset (flags, MDIODUMP_VERBOSE))
- {
- printf ("Access bits 31:16 using address 0x%08x:\n", address);
- }
- write_gbl_reg (&memmap, address, 1, command.data, command.mask);
- }
- else
- {
- if (_anyset (flags, MDIODUMP_VERBOSE))
- {
- printf ("Access bits 15:0 using address 0x%08x:\n", address);
- }
- write_gbl_reg (&memmap, address, 0, command.data, command.mask);
- }
- break;
- }
- if ((command.ctrl & 0x03) != 0x01)
- {
- error (1, ECANCELED, "start command must be 0x01");
- }
- if (_anyset (flags, MDIODUMP_VERBOSE))
- {
- printf ("\tStart: 0x%02x\n", command.ctrl & 0x03);
- printf ("\tOperation: 0x%02x (%s)\n", (command.ctrl & 0x0C) >> 2, ((command.ctrl & 0x0C) >> 2 == 0x01)? "write": "read");
- printf ("\tTurnaround: 0x%02x\n", (command.ctrl & 0xC000) >> 14);
- printf ("\tData: 0x%04x\n", command.data);
- printf ("\tMask: 0x%04x\n", command.mask);
- printf ("\n");
- }
- }
- if (_anyset (flags, MDIODUMP_SUMMARY))
- {
- printf ("Memory after execution:\n");
- print_memmap (&memmap);
- }
- free_memmap (&memmap);
- return (0);
- }
- /*====================================================================*
- *
- * signed phy_generic (char const * filename, unsigned commands, flag_t flags);
- *
- * assume instructions are 16-bit and display them in human readable
- * format on stdout;
- *
- *
- *--------------------------------------------------------------------*/
- static signed phy_generic (char const * filename, unsigned commands, flag_t flags)
- {
- struct command command;
- struct memmap memmap;
- if (init_memmap (commands, &memmap))
- {
- error (1, 0, "could not allocate memory for simulation");
- }
- while (commands--)
- {
- if (read (STDIN_FILENO, &command, sizeof (command)) != sizeof (command))
- {
- error (0, errno, FILE_CANTREAD, filename);
- return (-1);
- }
- command.ctrl = LE16TOH (command.ctrl);
- command.data = LE16TOH (command.data);
- command.mask = LE16TOH (command.mask);
- if ((command.ctrl & 0x03) != 0x01)
- {
- error (1, ECANCELED, "start command must be 0x01");
- }
- if (_anyset (flags, MDIODUMP_VERBOSE))
- {
- printf ("Start: 0x%02x\n", command.ctrl & 0x03);
- printf ("Operation: 0x%02x (%s)\n", (command.ctrl & 0x0C) >> 2, ((command.ctrl & 0x0C) >> 2 == 0x01)? "write": "read");
- printf ("Phy Address: 0x%02x\n", (command.ctrl & 0x1F0) >> 4);
- printf ("Register Address: 0x%02x\n", (command.ctrl & 0x3E00) >> 9);
- printf ("Turnaround: 0x%02x\n", (command.ctrl & 0xC000) >> 14);
- printf ("Data: 0x%04x\n", command.data);
- printf ("Mask: 0x%04x\n", command.mask);
- printf ("\n");
- continue;
- }
- if ((command.ctrl & 0x0C) >> 2 == 0x01)
- {
- if (_anyset (flags, MDIODUMP_SUMMARY))
- {
- write_phy_reg (&memmap, (command.ctrl & 0x1F0) >> 4, (command.ctrl & 0x3E00) >> 9, command.data, command.mask);
- continue;
- }
- print_command (&command);
- continue;
- }
- }
- if (_anyset (flags, MDIODUMP_SUMMARY))
- {
- printf ("Memory after execution:\n");
- print_memmap (&memmap);
- }
- free_memmap (&memmap);
- return (0);
- }
- /*====================================================================*
- *
- * signed function (char const * filename, unsigned phy_code, flag_t flags);
- *
- * read the MDIO block header to determine the number of MDIO
- * instructions in the program block; call appropriate function
- * to interpret instructions and display them in human readable
- * format;
- *
- *
- *--------------------------------------------------------------------*/
- static signed function (char const * filename, unsigned phy_code, flag_t flags)
- {
- uint16_t mdio_header;
- unsigned commands;
- if (read (STDIN_FILENO, &mdio_header, sizeof (mdio_header)) != sizeof (mdio_header))
- {
- error (0, errno, FILE_CANTREAD, filename);
- return (-1);
- }
- mdio_header = LE16TOH (mdio_header);
- commands = (mdio_header & 0xFFC0) >> 6;
- printf ("# ------- %s -------\n", filename);
- if (_anyset (flags, MDIODUMP_SUMMARY))
- {
- printf ("Enabled: %s\n", (mdio_header & 0x0001)? "yes": "no");
- printf ("Number of Commands: %d\n", commands);
- }
- if (phy_code == PHY_GENERIC)
- {
- return (phy_generic (filename, commands, flags));
- }
- if (phy_code == PHY_AR8236)
- {
- return (phy_ar8236 (filename, commands, flags));
- }
- return (0);
- }
- /*====================================================================*
- *
- * int main (int argc, const char * argv []);
- *
- *
- *
- *--------------------------------------------------------------------*/
- int main (int argc, const char * argv [])
- {
- static const char *optv [] =
- {
- "st:v",
- "file [file] [...]",
- "Atheros MDIO Custom Module Analyser",
- "s\tprint summary information",
- "t s\tinterpret MDIO commands for phy type (s) [generic]",
- "v\tprint complete module dump, not just the summary",
- (const char *) (0)
- };
- unsigned phy_code = PHY_GENERIC;
- flag_t flags = (flag_t)(0);
- signed state = 0;
- signed c;
- optind = 1;
- while ((c = getoptv (argc, argv, optv)) != -1)
- {
- switch ((char) (c))
- {
- case 's':
- _setbits (flags, MDIODUMP_SUMMARY);
- break;
- case 't':
- if ((c = lookup (optarg, switches, SIZEOF (switches))) == -1)
- {
- assist (optarg, "type", switches, SIZEOF (switches));
- }
- _setbits (flags, MDIODUMP_SUMMARY);
- phy_code = (unsigned)(c);
- break;
- case 'b':
- _clrbits (flags, MDIODUMP_SUMMARY);
- break;
- case 'v':
- _setbits (flags, MDIODUMP_VERBOSE);
- break;
- default:
- break;
- }
- }
- argc -= optind;
- argv += optind;
- if (!argc)
- {
- function ("stdin", phy_code, flags);
- }
- while ((argc) && (* argv))
- {
- if (!freopen (* argv, "rb", stdin))
- {
- error (0, errno, "%s", * argv);
- state = 1;
- errno = 0;
- }
- else if (function (* argv, phy_code, flags))
- {
- state = 1;
- }
- argc--;
- argv++;
- }
- return (state);
- }
- </pre>
- <div class='footerlink'>
- [<a href='mdioblock.c.html' title=' mdioblock.c '>PREV</a>]
- [<a href='toolkit.html' title=' Index '>HOME</a>]
- [<a href='mdiogen.c.html' title=' mdiogen.c '>NEXT</a>]
- </div>
- </body>
- </html>
|