📄 dm9161.c
字号:
/* ----------------------------------------------------------------------------
* ATMEL Microcontroller Software Support
* ----------------------------------------------------------------------------
* Copyright (c) 2008, Atmel Corporation
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Atmel's name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL ATMEL 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.
* ----------------------------------------------------------------------------
*/
// components/ethernet/dm9161/dm9161.c
//-----------------------------------------------------------------------------
// Headers
//-----------------------------------------------------------------------------
#include "dm9161.h"
#include "dm9161_define.h"
#include <pio/pio.h>
#include <rstc/rstc.h>
#include <emac/emac.h>
#include <utility/trace.h>
#include <utility/assert.h>
//-----------------------------------------------------------------------------
// Definitions
//-----------------------------------------------------------------------------
/// Default max retry count
#define DM9161_RETRY_MAX 100000
//-----------------------------------------------------------------------------
/// Dump all the useful registers
/// \param pDm Pointer to the Dm9161 instance
//-----------------------------------------------------------------------------
static void DM9161_DumpRegisters(Dm9161 * pDm)
{
unsigned char phyAddress;
unsigned int retryMax;
unsigned int value;
trace_LOG(trace_DEBUG, "DM9161_DumpRegisters\n\r");
ASSERT(pDm, "F: DM9161_DumpRegisters\n\r");
EMAC_EnableMdio();
phyAddress = pDm->phyAddress;
retryMax = pDm->retryMax;
trace_LOG(trace_INFO, "DM9161 (%d) Registers:\n\r", phyAddress);
EMAC_ReadPhy(phyAddress, DM9161_BMCR, &value, retryMax);
trace_LOG(trace_INFO, " _BMCR : 0x%X\n\r", value);
EMAC_ReadPhy(phyAddress, DM9161_BMSR, &value, retryMax);
trace_LOG(trace_INFO, " _BMSR : 0x%X\n\r", value);
EMAC_ReadPhy(phyAddress, DM9161_ANAR, &value, retryMax);
trace_LOG(trace_INFO, " _ANAR : 0x%X\n\r", value);
EMAC_ReadPhy(phyAddress, DM9161_ANLPAR, &value, retryMax);
trace_LOG(trace_INFO, " _ANLPAR : 0x%X\n\r", value);
EMAC_ReadPhy(phyAddress, DM9161_ANER, &value, retryMax);
trace_LOG(trace_INFO, " _ANER : 0x%X\n\r", value);
EMAC_ReadPhy(phyAddress, DM9161_DSCR, &value, retryMax);
trace_LOG(trace_INFO, " _DSCR : 0x%X\n\r", value);
EMAC_ReadPhy(phyAddress, DM9161_DSCSR, &value, retryMax);
trace_LOG(trace_INFO, " _DSCSR : 0x%X\n\r", value);
EMAC_ReadPhy(phyAddress, DM9161_10BTCSR, &value, retryMax);
trace_LOG(trace_INFO, " _10BTCSR: 0x%X\n\r", value);
EMAC_ReadPhy(phyAddress, DM9161_PWDOR, &value, retryMax);
trace_LOG(trace_INFO, " _PWDOR : 0x%X\n\r", value);
EMAC_ReadPhy(phyAddress, DM9161_CONFIGR, &value, retryMax);
trace_LOG(trace_INFO, " _CONFIGR: 0x%X\n\r", value);
EMAC_ReadPhy(phyAddress, DM9161_MDINTR, &value, retryMax);
trace_LOG(trace_INFO, " _MDINTR : 0x%X\n\r", value);
EMAC_ReadPhy(phyAddress, DM9161_RECR, &value, retryMax);
trace_LOG(trace_INFO, " _RECR : 0x%X\n\r", value);
EMAC_ReadPhy(phyAddress, DM9161_DISCR, &value, retryMax);
trace_LOG(trace_INFO, " _DISCR : 0x%X\n\r", value);
EMAC_ReadPhy(phyAddress, DM9161_RLSR, &value, retryMax);
trace_LOG(trace_INFO, " _RLSR : 0x%X\n\r", value);
EMAC_DisableMdio();
}
//-----------------------------------------------------------------------------
/// Find a valid PHY Address ( from 0 to 31 ).
/// Check BMSR register ( not 0 nor 0xFFFF )
/// Return 0xFF when no valid PHY Address found.
/// \param pDm Pointer to the Dm9161 instance
//-----------------------------------------------------------------------------
static unsigned char DM9161_FindValidPhy(Dm9161 * pDm)
{
unsigned int retryMax;
unsigned int value=0;
unsigned char rc;
unsigned char phyAddress;
unsigned char cnt;
trace_LOG(trace_DEBUG, "DM9161_FindValidPhy\n\r");
ASSERT(pDm, "F: DM9161_FindValidPhy\n\r");
EMAC_EnableMdio();
phyAddress = pDm->phyAddress;
retryMax = pDm->retryMax;
// Check current phyAddress
rc = phyAddress;
if( EMAC_ReadPhy(phyAddress, DM9161_PHYID1, &value, retryMax) == 0 ) {
trace_LOG(trace_INFO, "DM9161 PROBLEM\n\r");
}
trace_LOG(trace_DEBUG, " _PHYID1 : 0x%X, addr: %d\n\r", value, phyAddress);
// Find another one
if (value != DM9161_OUI_MSB) {
rc = 0xFF;
for(cnt = 0; cnt < 32; cnt ++) {
phyAddress = (phyAddress + 1) & 0x1F;
if( EMAC_ReadPhy(phyAddress, DM9161_PHYID1, &value, retryMax) == 0 ) {
trace_LOG(trace_INFO, "DM9161 PROBLEM\n\r");
}
trace_LOG(trace_DEBUG, " _PHYID1 : 0x%X, addr: %d\n\r", value, phyAddress);
if (value == DM9161_OUI_MSB) {
rc = phyAddress;
break;
}
}
}
EMAC_DisableMdio();
if (rc != 0xFF) {
trace_LOG(trace_INFO, "** Valid PHY Found: %d\n\r", rc);
EMAC_ReadPhy(phyAddress, DM9161_DSCSR, &value, retryMax);
trace_LOG(trace_DEBUG, " _DSCSR : 0x%X, addr: %d\n\r", value, phyAddress);
}
return rc;
}
//-----------------------------------------------------------------------------
// Exported functions
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
/// Setup the maximum timeout count of the driver.
/// \param pDm Pointer to the Dm9161 instance
/// \param toMax Timeout maxmum count.
//-----------------------------------------------------------------------------
void DM9161_SetupTimeout(Dm9161 *pDm, unsigned int toMax)
{
ASSERT(pDm, "F: DM9161_SetupTimeout\n\r");
pDm->retryMax = toMax;
}
//-----------------------------------------------------------------------------
/// Initialize the Dm9161 instance
/// \param pDm Pointer to the Dm9161 instance
/// \param pEmac Pointer to the Emac instance for the Dm9161
/// \param phyAddress The PHY address used to access the PHY
/// ( pre-defined by pin status on PHY reset )
//-----------------------------------------------------------------------------
void DM9161_Init(Dm9161 *pDm, unsigned char phyAddress)
{
ASSERT(pDm , "F: DM9161_Init\n\r");
pDm->phyAddress = phyAddress;
// Initialize timeout by default
pDm->retryMax = DM9161_RETRY_MAX;
}
//-----------------------------------------------------------------------------
/// Issue a SW reset to reset all registers of the PHY
/// Return 1 if successfully, 0 if timeout.
/// \param pDm Pointer to the Dm9161 instance
//-----------------------------------------------------------------------------
static unsigned char DM9161_ResetPhy(Dm9161 *pDm)
{
unsigned int retryMax;
unsigned int bmcr = DM9161_RESET;
unsigned char phyAddress;
unsigned int timeout = 10;
unsigned char ret = 1;
ASSERT(pDm, "F: DM9161_ResetPhy");
trace_LOG(trace_INFO, " DM9161_ResetPhy\n\r");
phyAddress = pDm->phyAddress;
retryMax = pDm->retryMax;
EMAC_EnableMdio();
bmcr = DM9161_RESET;
EMAC_WritePhy(phyAddress, DM9161_BMCR, bmcr, retryMax);
do {
EMAC_ReadPhy(phyAddress, DM9161_BMCR, &bmcr, retryMax);
timeout--;
} while ((bmcr & DM9161_RESET) && timeout);
EMAC_DisableMdio();
if (!timeout) {
ret = 0;
}
return( ret );
}
//-----------------------------------------------------------------------------
/// Do a HW initialize to the PHY ( via RSTC ) and setup clocks & PIOs
/// This should be called only once to initialize the PHY pre-settings.
/// The PHY address is reset status of CRS,RXD[3:0] (the emacPins' pullups).
/// The COL pin is used to select MII mode on reset (pulled up for Reduced MII)
/// The RXDV pin is used to select test mode on reset (pulled up for test mode)
/// The above pins should be predefined for corresponding settings in resetPins
/// The EMAC peripheral pins are configured after the reset done.
/// Return 1 if RESET OK, 0 if timeout.
/// \param pDm Pointer to the Dm9161 instance
/// \param mck Main clock setting to initialize clock
/// \param resetPins Pointer to list of PIOs to configure before HW RESET
/// (for PHY power on reset configuration latch)
/// \param nbResetPins Number of PIO items that should be configured
/// \param emacPins Pointer to list of PIOs for the EMAC interface
/// \param nbEmacPins Number of PIO items that should be configured
//-----------------------------------------------------------------------------
unsigned char DM9161_InitPhy(Dm9161 *pDm,
unsigned int mck,
const Pin *pResetPins,
unsigned int nbResetPins,
const Pin *pEmacPins,
unsigned int nbEmacPins)
{
AT91S_RSTC * pRSTC = AT91C_BASE_RSTC;
unsigned char rc = 1;
unsigned char phy;
ASSERT(pDm, "F: DM9161_InitPhy\n\r");
// Perform RESET
trace_LOG(trace_DEBUG, "RESET PHY\n\r");
if (pResetPins) {
// Configure PINS
PIO_Configure(pResetPins, nbResetPins);
// Execute reset
RSTC_SetExtResetLength(pRSTC, DM9161_RESET_LENGTH);
RSTC_ExtReset(AT91C_BASE_RSTC);
// Wait for end hardware reset
while (!RSTC_GetNrstLevel(pRSTC));
}
// Configure EMAC runtime pins
if (rc) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -