##############################################################################
#                                                                            #
# IAR ARM ANSI C/C++ Compiler V4.11A/W32               20/Jul/2004  18:58:51 #
# Copyright 1999-2004 IAR Systems. All rights reserved.                      #
#                                                                            #
#    Cpu mode        =  arm                                                  #
#    Endian          =  little                                               #
#    Stack alignment =  4                                                    #
#    Source file     =  C:\medusa\zl_mii.c                                   #
#    Command line    =  -I "C:\Program Files\IAR Systems\Embedded Workbench  #
#                       4.0\ARM\INC\" -lc C:\medusa\Debug\List\ -la          #
#                       C:\medusa\Debug\List\ -o C:\medusa\Debug\Obj\ -s2    #
#                       --no_cse --no_unroll --no_inline --no_code_motion    #
#                       --no_tbaa --no_clustering --no_scheduling --debug    #
#                       --cpu_mode arm --endian little --cpu ARM7TDMI-S      #
#                       --stack_align 4 -e --fpu None                        #
#                       "-D_DLIB_CONFIG_FILE=C:\Program Files\IAR            #
#                       Systems\Embedded Workbench                           #
#                       4.0\ARM\LIB\dl4tpannl8n.h" C:\medusa\zl_mii.c        #
#    List file       =  C:\medusa\Debug\List\zl_mii.lst                      #
#    Object file     =  C:\medusa\Debug\Obj\zl_mii.r79                       #
#                                                                            #
#                                                                            #
##############################################################################

C:\medusa\zl_mii.c
      1          #include "archinc.h"
      2          #include "cli.h"
      3          #include "strutil.h"
      4          #include "zarlink.h"
      5          
      6          #ifdef CONFIG_INCLUDE_ZARLINK
      7          
      8          #include <stdio.h>
      9          #include <stdlib.h>
     10          
     11          typedef struct {
     12            int offset;
     13            char * name;
     14            } miiReg_t;
     15          
     16          static miiReg_t miiRegs[12] = {{0x00, "Basic Control   "},
     17                                         {0x01, "Basic Status    "},
     18                                         {0x02, "PHY Id 1        "},
     19                                         {0x03, "PHY Id 2        "},
     20                                         {0x04, "Autoneg Advert  "},
     21                                         {0x05, "Autoneg Partner "},
     22                                         {0x06, "Autoneg Expans  "},
     23                                         {0x07, "Autoneg Next Pg "},
     24                                         {0x08, "Lnk Prt Next Pg "},
     25                                         {0x15, "RXER Counter    "},
     26                                         {0x1b, "IRQ Ctrl/Status "},
     27                                         {0x1f, "100BaseTX PHY   "}};
     28          
     29          /*
     30           * Zarlink MII interface write routine
     31           *
     32           * 1. set MII Management State Machine Disable (bit[6]) in FEN register;
     33           * 2. wait until RDY is set (bit [7]) in MIIC3;
     34           * 3. set low bit 8 of new MII register value in MIIC0;
     35           * 4. set high bit 8 of new MII register value in MIIC1;
     36           * 5. set (0x20 | (reg_num & 0x1F)) in MIIC2;
     37           * 6. set (phy_addr & 0x1F) in MIIC3;
     38           * 7. unset MII Management State Machine Disable (bit [6]) in FEN;
     39           *
     40           */
     41          
     42          int zarlinkMiiWrite(unsigned char miiPhy, unsigned char miiReg, unsigned short miiCmd)
     43          {
     44            unsigned char tmpReg;
     45            unsigned char fenShadow;
     46            int i;
     47          
     48            fenShadow = zarlinkIndexedRead(0x602);
     49            zarlinkIndexedWrite(0x602, (fenShadow | 0x40));
     50          
     51            for(i=0;i<1000;i++) {
     52              tmpReg = zarlinkIndexedRead(0x606);
     53              if ((tmpReg & 0x80) != 0)
     54                break;
     55              }
     56          
     57            if ((tmpReg & 0x80) == 0)
     58              return -1;
     59          
     60            zarlinkIndexedWrite(0x603, (unsigned char)(miiCmd & 0x00FF));
     61            zarlinkIndexedWrite(0x604, (unsigned char)((miiCmd >> 8) & 0x00FF));
     62            zarlinkIndexedWrite(0x605, (unsigned char)(0x20 | (miiReg & 0x1F)));
     63            zarlinkIndexedWrite(0x606, (miiPhy & 0x1F));
     64          
     65            zarlinkIndexedWrite(0x602, fenShadow);
     66          
     67            return 0;
     68          }
     69          
     70          /* 
     71           * Zarlink MII interface read routine
     72           *
     73           * 1. set MII Management State Machine Disable (bit [6]) in FEN register;
     74           * 2. wait until RDY is set (bit [7]) in MIIC3;
     75           * 3. set (0x40 | (reg_num & 0x1F)) in MIIC2;
     76           * 4. set (phy_addr & 0x1F) in MIIC3;
     77           * 5. wait until RDY (bit [7]) AND VALID (bit [6]) are set in MIIC3;
     78           * 6. read low bit 8 of new_value in MIID0;
     79           * 7. read high bit 8 of new_value in MIID1;
     80           * 8. unset MII Management State Machine Disable (bit [6]) in FEN;
     81           *
     82           */
     83          
     84          int zarlinkMiiRead(unsigned char miiPhy, unsigned char miiReg, unsigned short * pMiiData)
     85          {
     86            unsigned short result;
     87            unsigned char tmpReg;
     88            unsigned char fenShadow;
     89            int i;
     90          
     91            fenShadow = zarlinkIndexedRead(0x602);
     92            zarlinkIndexedWrite(0x602, (fenShadow | 0x40));
     93          
     94            for(i=0;i<1000;i++) {
     95              tmpReg = zarlinkIndexedRead(0x606);
     96              if ((tmpReg & 0x80) == 0x80)
     97                break;
     98              }
     99          
    100            if ((tmpReg & 0x80) == 0)
    101              return -1;
    102            
    103            zarlinkIndexedWrite(0x605, (0x40 | (miiReg & 0x1F)));
    104            zarlinkIndexedWrite(0x606, (miiPhy & 0x1F));
    105          
    106            for(i=0;i<1000;i++) {
    107              tmpReg = zarlinkIndexedRead(0x606);
    108              if ((tmpReg & 0xC0) == 0xC0)
    109                break;
    110              }
    111          
    112            if ((tmpReg & 0xC0) != 0xC0)
    113                return -2;
    114          
    115            result = (unsigned short)zarlinkIndexedRead(0x607);
    116            result |= (unsigned short)((zarlinkIndexedRead(0x608) << 8) & 0xff00);
    117          
    118            zarlinkIndexedWrite(0x602, fenShadow);
    119          
    120            *pMiiData = result;
    121            return 0;
    122          }
    123          
    124          int zarlinkMiiDump(int phy)
    125          {
    126            char * pOutBuf;
    127            int bufLen;
    128            int i;
    129            unsigned short phyData;
    130            int retVal;
    131          
    132            if ((phy < 8) || (phy > 13))
    133              return retVal;
                            ^
Warning[Pe549]: variable "retVal" is used before its value is set
    134          
    135            pOutBuf = clicharGetOutputBuffer(&bufLen);
    136          
    137            sprintf(pOutBuf, "\r\ndumping all regs for phy [%d]\r\n", phy);
    138            clicharSendOutputBuffer(pOutBuf, bufLen);
    139            
    140            retVal = 0;
    141            for(i=0;((i<12) && (retVal == 0));i++) {
    142              retVal = zarlinkMiiRead((unsigned char)phy, (unsigned char)(miiRegs[i].offset), &phyData);
    143              if (retVal == 0) {
    144                sprintf(pOutBuf, "%s [%04x]\r\n", miiRegs[i].name, phyData);
    145                clicharSendOutputBuffer(pOutBuf, bufLen);
    146                }
    147              }
    148          
    149            return retVal;
    150          }
    151          
    152          // look at a phy and if it's not up, put it into 'power save' mode
    153          // if return value is zero, then no action was taken. If it's 1, 
    154          // then the PHY has been powered down
    155          
    156          int zarlinkMiiPowerSave(int phy)
    157          {
    158            unsigned short phyData;
    159            int retVal = -1;
    160          
    161            if ((phy < 8) || (phy > 13))
    162              return retVal;
    163          
    164            retVal = zarlinkMiiRead((unsigned char)phy, (unsigned char)0x01, &phyData);
    165          
    166            if (retVal == 0) {
    167              if ((phyData & 0x0004) == 0) {
    168                // link is down
    169                retVal = zarlinkMiiWrite((unsigned char)phy, (unsigned char)0x00, (unsigned short)0x0800);
    170                if (retVal == 0)
    171                  retVal = 1;
    172                }
    173              else {
    174                retVal = 0;
    175                // link is up
    176                }
    177              }
    178          
    179            return retVal;
    180          
    181          }
    182          
    183          static int phypwrHandler(int argc, char *argv[], int flags)
    184          {
    185            int retVal = -1;
    186            unsigned short phyData;
                                  ^
Warning[Pe177]: variable "phyData" was declared but never referenced
    187            unsigned char phyAddr;
                                 ^
Warning[Pe177]: variable "phyAddr" was declared but never referenced
    188            unsigned char phyReg; 
                                 ^
Warning[Pe177]: variable "phyReg" was declared but never referenced
    189            char * pOutBuf;
                          ^
Warning[Pe550]: variable "pOutBuf" was set but never used
    190            int bufLen;
    191          
    192            pOutBuf = clicharGetOutputBuffer(&bufLen);
    193          
    194            if (argc == 2) {
    195              }
    196            else if (argc == 1) {
    197              }
    198          
    199            return retVal;
    200          }
    201          
    202          static int defaultPhy = -1;
    203          
    204          static int miiHandler(int argc, char * argv[], int flags)
    205          {
    206            int retVal = -1;
    207            int i;
    208            unsigned short phyData;
    209            unsigned char phyAddr;
                                 ^
Warning[Pe177]: variable "phyAddr" was declared but never referenced
    210            unsigned char phyReg; 
    211            char * pOutBuf;
    212            int bufLen;
    213          
    214            pOutBuf = clicharGetOutputBuffer(&bufLen);
    215          
    216            switch(*argv[1]) {
    217              case 'p':
    218              case 'P':
    219                if (argc == 3) {
    220                  defaultPhy = atoi(argv[2]);
    221                  sprintf(pOutBuf, "mii default phy set to [%02x]\r\n", defaultPhy);
    222                  retVal = 0;
    223                  }
    224                 // set default phy
    225                 break;
    226              case 'r':
    227              case 'R':
    228                if (defaultPhy == -1) {
    229                  sprintf(pOutBuf, "mii default phy not set, please use mii [phy <defaultPhy>]\r\n");
    230                  break;
    231                  }
    232                phyReg = (unsigned char)strtol(argv[2], NULL, 16);
    233                retVal = zarlinkMiiRead(((unsigned char)defaultPhy), phyReg, &phyData);
    234                if (retVal == 0) {
    235                  sprintf(pOutBuf, "mii read phy[%02x] reg[%02x] = %04x  OK\r\n", defaultPhy, phyReg, phyData);
    236                  }
    237                else {
    238                  sprintf(pOutBuf, "mii read phy[%02x] reg[%02x]  ERROR %d\r\n", defaultPhy, phyReg, retVal);
    239                  }
    240                break;
    241              case 'd':
    242              case 'D':
    243                if (defaultPhy == -1) {
    244                  sprintf(pOutBuf, "mii default phy not set, please use mii [phy <defaultPhy>]\r\n");
    245                  break;
    246                  }
    247                retVal = zarlinkMiiDump(((unsigned char)defaultPhy));
    248                break;
    249              case 'a':
    250              case 'A':
    251                retVal = 0;
    252                for(i=8;((i<14) && (retVal == 0)) ;i++) {
    253                  retVal = zarlinkMiiDump(((unsigned char)i));
    254                  }
    255                pOutBuf[0] = '\0';
    256                break;
    257              case 'w':
    258              case 'W':
    259                if (defaultPhy == -1) {
    260                  sprintf(pOutBuf, "mii default phy not set, please use mii [phy <defaultPhy>]\r\n");
    261                  break;
    262                  }
    263                phyReg = (unsigned char)strtol(argv[2], NULL, 16);
    264                phyData = (unsigned short)strtol(argv[3], NULL, 16);
    265                retVal = zarlinkMiiWrite(((unsigned char)defaultPhy), phyReg, phyData);
    266                if (retVal == 0) {
    267                  sprintf(pOutBuf, "mii write phy[%02x] reg[%02x] <-- %04x  OK\r\n", defaultPhy, phyReg, phyData);
    268                  }
    269                else {
    270                  sprintf(pOutBuf, "mii write phy[%02x] reg[%02x]  ERROR %d\r\n", defaultPhy, phyReg, retVal);
    271                  }
    272                break;
    273              default:
    274                break;
    275              }
    276          
    277            clicharSendOutputBuffer(pOutBuf, bufLen);
    278            return retVal;
    279          }
    280          
    281          int zarlinkMiiInit(void)
    282          {
    283            cliRegisterCommand("mii", miiHandler, "mii [(p)hy <defaultPhy>] [(r)ead <address>] [(w)rite <address> <data>] [(a)llregs] [(d)umpregs]");
    284            cliRegisterCommand("phypwr", phypwrHandler, "phypwr [phy] - enable power save mode if no link detected on a phy");
    285            return 0;
    286          }
    287          
    288          #endif
    289          

   Maximum stack usage in bytes:

     Function            CSTACK
     --------            ------
     atoi                    8
     miiHandler             44
     phypwrHandler          32
     zarlinkMiiDump         28
     zarlinkMiiInit          4
     zarlinkMiiPowerSave    16
     zarlinkMiiRead         32
     zarlinkMiiWrite        28


   Segment part sizes:

     Function/Label                 Bytes
     --------------                 -----
     atoi                             28
     miiRegs                          96
     zarlinkMiiWrite                 276
     zarlinkMiiRead                  360
     zarlinkMiiDump                  240
     zarlinkMiiPowerSave             160
     phypwrHandler                    52
     defaultPhy                        4
     miiHandler                      748
     zarlinkMiiInit                   68
     ?<Initializer for miiRegs>       96
     ?<Initializer for defaultPhy>     4
     ?<Constant "Basic Control   ">   20
     ?<Constant "Basic Status    ">   20
     ?<Constant "PHY Id 1        ">   20
     ?<Constant "PHY Id 2        ">   20
     ?<Constant "Autoneg Advert  ">   20
     ?<Constant "Autoneg Partner ">   20
     ?<Constant "Autoneg Expans  ">   20
     ?<Constant "Autoneg Next Pg ">   20
     ?<Constant "Lnk Prt Next Pg ">   20
     ?<Constant "RXER Counter    ">   20
     ?<Constant "IRQ Ctrl/Status ">   20
     ?<Constant "100BaseTX PHY   ">   20
     ?<Constant "\r\ndumping all regs fo...">
                                      36
     ?<Constant "%s [%04x]\r\n">      12
     ?<Constant "mii default phy set t...">
                                      32
     ?<Constant "mii default phy not s...">
                                      64
     ?<Constant "mii read phy[%02x] re...">
                                      44
     ?<Constant "mii read phy[%02x] re...">_1
                                      44
     ?<Constant "mii write phy[%02x] r...">
                                      48
     ?<Constant "mii write phy[%02x] r...">_1
                                      44
     ?<Constant "mii">                 4
     ?<Constant "mii [(p)hy <defaultPh...">
                                      96
     ?<Constant "phypwr">              8
     ?<Constant "phypwr [phy] - enable...">
                                      68
      Others                         120

 
 2 040 bytes in segment CODE
   740 bytes in segment DATA_C
   100 bytes in segment DATA_I
   100 bytes in segment DATA_ID
    12 bytes in segment INITTAB
 
 1 904 bytes of CODE  memory (+ 148 bytes shared)
   840 bytes of CONST memory
   100 bytes of DATA  memory

Errors: none
Warnings: 6
