⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mplmisc.c

📁 NATIONAL公司DP83816芯片Linux下驱动
💻 C
字号:

//******************************************************************************
//
//  MPLMISC.C
//
//  Copyright (c) 2004 National Semiconductor Corporation.
//  All Rights Reserved
//
//  MPL miscellaneous support APIs
//
//  This file contains the API implementations for
//     o Set/Get of Mac Address
//     o Controlling loopback mode
//     o Set/Clear of packet priority
//
//******************************************************************************

#include <mplinternal.h>

// Local helful debug macros
#undef ENTER
#undef EXIT
#undef ASSERT
#undef PRINT
#undef PRINTI
#undef PRINTS
#undef TBREAK

#define ENTER(fn) MPL_CENTER(DZONE_MPL_MISC, fn)
#define EXIT(fn) MPL_CEXIT(DZONE_MPL_MISC, fn)
#define ASSERT(le) MPL_CASSERT(DZONE_MPL_MISC, le)
#define PRINT(fmt) MPL_CTRACE(DZONE_MPL_MISC, fmt)
#define PRINTI(v) MPL_CTRACE(DZONE_MPL_MISC, (" "#v": %x \n",(NS_UINT)(v)))
#define PRINTS(v) MPL_CTRACE(DZONE_MPL_MISC, (" "#v": %s \n", v))
#define TBREAK(fmt) MPL_CTRACEBREAK(DZONE_MPL_MISC, fmt)

//*****************************************************************************
//   MplSetMacAddress
//      Set the MAC's address on the device
//
//   Parameters
//      pMplHandle
//         MPL device handle returned following a call to MplInitialize
//      pMacAddr
//         Pointer to caller supplied variable with the new MAC address
//
//   Return Value
//      NS_STATUS_SUCCESS
//         The MAC address was sucessfully programmed on the device
//
//*****************************************************************************
MPL_STATUS
   MplSetMacAddress (
      IN NS_VOID         *pMplHandle,
      IN MPL_MAC_ADDR     pMacAddr
      )
{
   MPL_CONTEXT *pMplCtx = (MPL_CONTEXT *)pMplHandle;
   NS_UINT16 *tmpPtr = (NS_UINT16 *) pMacAddr; 
   NS_UINT32 regVal;
   NS_UINT8 i;

   ENTER(MplSetMacAddress);

   // Disable Rx Filter
   regVal = MPL_READ32(pMplCtx, RFCR);
   MPL_WRITE32(pMplCtx, RFCR, regVal & ~RXFLTR_EN);

   // Enter new Mac addr
   for (i = 0x0; i < 0x3; i++, tmpPtr++)
   {
      // Set control bits
      MPL_WRITE32(pMplCtx, RFCR, i * 2);

      // Write actual data-16bits
      // No Swapping to be done for MAC addr since it should be N/w order
      //   So go direct to OAI API
      OaiIoWrite32(pMplCtx->pClientHandle,
                    (NS_UINT32 volatile *) &pMplCtx->pRegsBase->RFDR,
                      *tmpPtr);
   }

   // Reenable filter
   MPL_WRITE32(pMplCtx, RFCR, regVal);

   // Copy the mac address
   OaiMemCopy(pMplCtx->permMacAddr, pMacAddr, 0x06);

      return NS_STATUS_SUCCESS;
   EXIT(MplSetMacAddress);
   return NS_STATUS_SUCCESS;
}

//*****************************************************************************
//   MplGetMacAddress
//      Get the current MAC's address on the device
//
//   Parameters
//      pMplHandle
//         MPL device handle returned following a call to MplInitialize.
//      eeProm
//         If set to NS_TRUE, the permanent (EEPROM programmed) mac address 
//          for the device is returned back to the caller, else the current
//          active address is returned.
//      pMacAddr
//         Pointer to a caller supplied MPL_MAC_ADDR variable where the current
//          or default network address is returned.
//
//   Return Value
//      NS_STATUS_SUCCESS
//       The mac address was successfully read and is returned.
//      NS_STATUS_FAILURE
//       Failed to get the MAC address
//*****************************************************************************
MPL_STATUS
   MplGetMacAddress (
      IN NS_VOID        *pMplHandle,
      IN NS_BOOLEAN      eeProm,
      OUT MPL_MAC_ADDR   pMacAddr
      )
{
   MPL_CONTEXT *pMplCtx = (MPL_CONTEXT *)pMplHandle;
   MPL_STATUS status = NS_STATUS_SUCCESS;
   NS_UINT16 *tmpPtr = (NS_UINT16 *) pMacAddr; 
   NS_UINT32 regVal;
   NS_UINT8 i;

   ENTER(MplGetMacAddress);

   // Read from the source
   if (eeProm == NS_TRUE) 
   {
#ifndef MPL_NO_EEPROM
   {
      // MP3 differs from all other Mac relative to EEPROM layout
      if (pMplCtx->devId != MPL_DEVICE_ID_DP83818)
      {
         NS_UINT16 prevEE, currEE; 

         // Read from EEPROM
         status = MplEERead(pMplCtx, 6, &prevEE);
         for (i = 0; (i < 3) && (status == NS_STATUS_SUCCESS); i++) 
         {
            status = MplEERead(pMplCtx, i + 7, &currEE);
            pMacAddr[i*2] = (currEE << 1) + (prevEE >> 15);
            pMacAddr[i*2 + 1] = currEE >> 7;
            prevEE = currEE;
         }
      }
      else
      {
         // FM: Swapping needed?
         status = MplEERead(pMplCtx, 0xA, tmpPtr);
         status = MplEERead(pMplCtx, 0xB, ++tmpPtr);
         status = MplEERead(pMplCtx, 0xC, ++tmpPtr);
      }
   }
#else
      status = NS_STATUS_FAILURE; // No EEPROM on this board
#endif// MPL_NO_EEPROM

   }
   else
   {
      // Read from MAC Registers

      // Disable Rx Filter
      regVal = MPL_READ32(pMplCtx, RFCR);
      MPL_WRITE32(pMplCtx, RFCR, regVal & ~RXFLTR_EN);

      // Read the current Mac Addr (if this function was called following a load
      // from EEPROM operation then the addr visible will be the permanent addr)
      for (i = 0x0; i < 0x3; i++, tmpPtr++)
      {
         // Set control bits
         MPL_WRITE32(pMplCtx, RFCR, i * 2);

         // Read actual data-16bits
         // No Swapping to be done for MAC addr since it should be N/w order
         //   So go direct to OAI API
         *tmpPtr = (NS_UINT16)OaiIoRead32(pMplCtx->pClientHandle,
                       (NS_UINT32 volatile *) &pMplCtx->pRegsBase->RFDR);
      }

      // Reenable filter
      MPL_WRITE32(pMplCtx, RFCR, regVal);
   }

   EXIT(MplGetMacAddress);
   return status;
}

//*****************************************************************************
//   MplGetDeviceId
//      Get the device Id to identify the MacPhyter version
//
//   Parameters
//      pMplHandle
//         MPL device handle returned following a call to MplInitialize.
//
//   Return Value
//      MPL_DEVICE_ID_DP83815 = Macphyter-I found
//      MPL_DEVICE_ID_DP83816 = Macphyter-II found
//      MPL_DEVICE_ID_DP83818 = Macphyter-III found
//*****************************************************************************
MPL_DEVICE_ID
   MplGetDeviceId(
      IN NS_VOID        *pMplHandle
      )
{
   MPL_CONTEXT *pMplCtx = (MPL_CONTEXT *)pMplHandle;
   NS_UINT32 regVal;

   ENTER(MplGetDeviceId);

   // Read the SRR
   regVal = MPL_READ32(pMplCtx, SRR);
   pMplCtx->srr = regVal;
   switch (regVal)
   {
      case MPI_C_MAC_ID   :
      case MPI_D_MAC_ID   : pMplCtx->devId = MPL_DEVICE_ID_DP83815;
                            break;
      case MPII_A4_MAC_ID : 
      case MPII_A5_MAC_ID : pMplCtx->devId = MPL_DEVICE_ID_DP83816;
                            break;
      case MPIII_MAC_ID   : pMplCtx->devId = MPL_DEVICE_ID_DP83818;
                            break;
      default             : pMplCtx->devId = MPL_DEVICE_ID_UNKNOWN;
   }

   // Get MPL caps
   MplGetCaps(pMplCtx, &pMplCtx->caps);

   EXIT(MplGetDeviceId);
   return pMplCtx->devId;
}

//*****************************************************************************
//   MplGetRegs
//      Get the current operational registers values
//
//   Parameters
//      pMplHandle
//         MPL device handle returned following a call to MplInitialize.
//      pRegs
//         A caller supplied buffer array (atleast 64*4 bytes)
//
//   Return Value
//      None
//*****************************************************************************
NS_VOID
   MplGetRegs(
      IN NS_VOID        *pMplHandle,
      IN NS_UINT32      *pRegs
      )
{
   MPL_CONTEXT *pMplCtx = (MPL_CONTEXT *)pMplHandle;
   NS_UINT32 i;
   NS_UINT32 volatile *regAddr;

   ENTER(MplGetRegs);

   // Get the Page-0 registers
   for (i = 0x0; i < 64; i++)
   {
      regAddr = (NS_UINT32 volatile *)((NS_UINT8 *)pMplCtx->pRegsBase + i*4);
      pRegs[i] = MPL_LTOH32(OaiIoRead32(pMplCtx->pClientHandle, regAddr));
   }

   EXIT(MplGetRegs);
   return;
}

//*****************************************************************************
//   MplDumpTransmitRing
//      Dump the Txd ring
//
//   Parameters
//      pMplHandle
//         MPL device handle returned following a call to MplInitialize.
//
//   Return Value
//      None
//*****************************************************************************
NS_VOID
   MplDumpTransmitRing(
      IN NS_VOID        *pMplHandle
      )
{
   MPL_CONTEXT *pMplCtx = (MPL_CONTEXT *)pMplHandle;
   NS_UINT i, j;

   ENTER(MplDumpTransmitRing);

   for (i = 0x0; i < pMplCtx->txQueueCnt; i++)
   {
      PRINT(("Dumping Tx Queue: %d \n", i));

      PRINT(("Curr: %d  Dirty: %d \n", pMplCtx->txQueue[i].curr,
                                       pMplCtx->txQueue[i].dirty));

      PRINT(("Ring Phy Addr: %x  Virtual Addr: %x \n", 
                    pMplCtx->txQueue[i].ringPA, 
                      (NS_ADDR)pMplCtx->txQueue[i].pRing));

      for (j = 0x0; j < pMplCtx->txQueue[i].descCnt; j++)
      {
         PRINT(("Txd: %d Next: %x CmdSts: %x BufPtr: %x MplPriv: %x \n", 
                  j, pMplCtx->txQueue[i].pRing[j].linkPA, 
                  (NS_UINT)pMplCtx->txQueue[i].pRing[j].cmdSts, 
                  (NS_ADDR)pMplCtx->txQueue[i].pRing[j].bufptrPA,
                  (NS_ADDR)pMplCtx->txQueue[i].pRing[j].mplPriv)); 
      }
   }

   EXIT(MplDumpTransmitRing);
   return;
}

//*****************************************************************************
//   MplDumpReceiveRing
//      Dump the Rxd ring
//
//   Parameters
//      pMplHandle
//         MPL device handle returned following a call to MplInitialize.
//
//   Return Value
//      None
//*****************************************************************************
NS_VOID
   MplDumpReceiveRing(
      IN NS_VOID        *pMplHandle
      )
{
   MPL_CONTEXT *pMplCtx = (MPL_CONTEXT *)pMplHandle;
   NS_UINT j;

   ENTER(MplDumpReceiveRing);

   PRINT(("Dumping Rx Queue \n"));
   PRINT(("Curr: %d  Dirty: %d \n", pMplCtx->rxQueue.curr,
                                    pMplCtx->rxQueue.dirty));

   PRINT(("Ring Phy Addr: %x  Virtual Addr: %x \n", 
                 pMplCtx->rxQueue.ringPA, (NS_ADDR)pMplCtx->rxQueue.pRing));

   for (j = 0x0; j < pMplCtx->rxQueue.descCnt; j++)
   {
      PRINT(("Rxd: %d Next: %x CmdSts: %x BufPtr: %x MplPriv: %x \n", 
               j, pMplCtx->rxQueue.pRing[j].linkPA, 
               (NS_UINT)pMplCtx->rxQueue.pRing[j].cmdSts, 
               (NS_ADDR)pMplCtx->rxQueue.pRing[j].bufptrPA, 
               (NS_ADDR)pMplCtx->rxQueue.pRing[j].mplPriv)); 
   }

   EXIT(MplDumpReceiveRing);
   return;
}

//*****************************************************************************
//   MplRegRead
//      Read a 32-bit register
//
//   Parameters
//      pMplHandle
//         MPL device handle
//      regIndex
//         Register to read (in terms of absolute offset e.g. CR = 0x0, 
//                             TXCFG = 0x24)
//
//   Return Value
//     Reg data
//
//*****************************************************************************
NS_UINT32
   MplRegRead(
      IN NS_VOID   *pMplHandle,
      IN NS_UINT    regIndex
      )
{
   MPL_CONTEXT *pMplCtx = (MPL_CONTEXT *)pMplHandle;
   NS_UINT32 volatile *regAddr;

   regAddr = (NS_UINT32 volatile *) ((NS_UINT8 *)pMplCtx->pRegsBase + 
                                                    regIndex);
   return MPL_LTOH32(OaiIoRead32(pMplCtx->pClientHandle, regAddr));
}

//*****************************************************************************
//   MplRegWrite
//      Write a 32-bit register
//
//   Parameters
//      pMplHandle
//         MPL device handle
//      regIndex
//         Register to read (in terms of absolute offset e.g. CR = 0x0, 
//                             TXCFG = 0x24)
//      regData
//         Data to write
//
//   Return Value
//     None
//
//*****************************************************************************
NS_VOID
   MplRegWrite(
      IN NS_VOID   *pMplHandle,
      IN NS_UINT    regIndex,
      IN NS_UINT32  regData
      )
{
   MPL_CONTEXT *pMplCtx = (MPL_CONTEXT *)pMplHandle;
   NS_UINT32 volatile *regAddr;

   regAddr = (NS_UINT32 volatile *) ((NS_UINT8 *)pMplCtx->pRegsBase + 
                                                    regIndex);

   OaiIoWrite32(pMplCtx->pClientHandle, regAddr, MPL_HTOL32(regData));
   return;
}

#ifndef MPL_NO_EEPROM
//*****************************************************************************
//   MplEERead
//     Read 16-bit word from a given EEPROM offset 
//
//   Parameters
//      pMplHandle
//         MPL device handle returned following a call to MplInitialize.
//      index
//         Offset from where to read EEPROM contents
//     *pData
//         Caller provided variable where the 16-bit contents is returned
//
//   Return Value
//      NS_STATUS_SUCCESS
//       The EEPROM was successfully read
//      NS_STATUS_INVALID_PARM
//       The EEPROM offset is invalid
//*****************************************************************************
MPL_STATUS
   MplEERead(
      IN NS_VOID    *pMplHandle,
      IN NS_UINT     index,
      IN NS_UINT16  *pData   
      )
{
   MPL_CONTEXT *pMplCtx = (MPL_CONTEXT *)pMplHandle;
   NS_UINT32 cmd, regVal;
   NS_SINT i;
   NS_UINT16 data = 0x0;

   ENTER(MplEERead);

   // Select EE chip
   MPL_WRITE32(pMplCtx, MEAR, EEPROM_CS);

   // Write out the read command, one bit at a time, insuring that each 
   // transition of the clock lasts at least 2 microseconds.
   cmd = EEPROM_READ | index;
   for (i = 10; i >= 0; i--) 
   {
      regVal = (cmd & (1 << i)) ? EEPROM_CS | EEPROM_DI : EEPROM_CS;
      MPL_WRITE32(pMplCtx, MEAR, regVal);
      MPL_READ32(pMplCtx, MEAR); //OaiSleep(4);
      MPL_WRITE32(pMplCtx, MEAR, (regVal | EEPROM_CLK));
      MPL_READ32(pMplCtx, MEAR); //OaiSleep(4);
   }

   MPL_WRITE32(pMplCtx, MEAR, EEPROM_CS);
   MPL_READ32(pMplCtx, MEAR); //OaiSleep(4);

   // Read in the 16 bits of data, one bit at a time, insuring that each
   // transition of the clock lasts at least 2 microseconds.
   for (i = 0; i < 16; i++) 
   {
      MPL_WRITE32(pMplCtx, MEAR, EEPROM_CS | EEPROM_CLK);
      MPL_READ32(pMplCtx, MEAR); //OaiSleep(4);
      data |= ((MPL_READ32(pMplCtx, MEAR) & EEPROM_DO) ? 1 << i : 0);
      MPL_WRITE32(pMplCtx, MEAR, EEPROM_CS);
      MPL_READ32(pMplCtx, MEAR); //OaiSleep(4);
   }

   // Close the EEPROM
   MPL_WRITE32(pMplCtx, MEAR, EEPROM_CS);
   MPL_WRITE32(pMplCtx, MEAR, 0x0);

  *pData = data;

   EXIT(MplEERead);
   return NS_STATUS_SUCCESS;
}
#endif// MPL_NO_EEPROM

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -